mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-22 17:37:38 +00:00
Compare commits
64 Commits
v1.16.4-1.
...
v1.16.5-1.
Author | SHA1 | Date | |
---|---|---|---|
![]() |
efa2be2821 | ||
![]() |
670db97fc7 | ||
![]() |
1650b72edb | ||
![]() |
a5bca3f0df | ||
![]() |
88f41314c7 | ||
![]() |
5ef8d52c13 | ||
![]() |
0b65d56ab0 | ||
![]() |
a256b70685 | ||
![]() |
f16d1499fe | ||
![]() |
79ca851e4f | ||
![]() |
d5c54d64a6 | ||
![]() |
5cfdd2339f | ||
![]() |
3ab3213290 | ||
![]() |
46c9840d00 | ||
![]() |
b3f2f14e96 | ||
![]() |
3ace49d27f | ||
![]() |
9bd662d8dc | ||
![]() |
df7a40354e | ||
![]() |
c489d4bc4f | ||
![]() |
2b029bd506 | ||
![]() |
2227845658 | ||
![]() |
2d3e88ef59 | ||
![]() |
0bfe960cbd | ||
![]() |
a735f23e1f | ||
![]() |
de6f27ceaf | ||
![]() |
2fab1a3054 | ||
![]() |
d4745ae47e | ||
![]() |
dc21e2dbc9 | ||
![]() |
75dfa71275 | ||
![]() |
d71bf225cc | ||
![]() |
8644c4ebf6 | ||
![]() |
b323db30ee | ||
![]() |
53efd6b303 | ||
![]() |
97faa1b3bc | ||
![]() |
7404133d40 | ||
![]() |
e18e24407e | ||
![]() |
026afa7f73 | ||
![]() |
29cc5bb86b | ||
![]() |
aa9d3c8269 | ||
![]() |
f8074636bc | ||
![]() |
db2cde4a4c | ||
![]() |
5eec7d9172 | ||
![]() |
8b9735d72e | ||
![]() |
1866916cb8 | ||
![]() |
f38a6a9d43 | ||
![]() |
0f6db63020 | ||
![]() |
51fcd83b87 | ||
![]() |
c2190e1318 | ||
![]() |
c40a13558c | ||
![]() |
02695aea51 | ||
![]() |
d5be1aca0e | ||
![]() |
8ff8b78ed8 | ||
![]() |
7fc55aa9a0 | ||
![]() |
38335ca187 | ||
![]() |
e0e194099c | ||
![]() |
8063059764 | ||
![]() |
f96d923b2a | ||
![]() |
9142ccfc93 | ||
![]() |
9f7cc00fcb | ||
![]() |
b129ae627b | ||
![]() |
f9fb0619fa | ||
![]() |
7f9b86a78e | ||
![]() |
58ea7a275e | ||
![]() |
8487a13764 |
12
.gitattributes
vendored
12
.gitattributes
vendored
@@ -1,3 +1,15 @@
|
||||
# Ignore changes in generated files
|
||||
src/generated/resources/data/** linguist-generated
|
||||
src/test/server-files/structures linguist-generated
|
||||
|
||||
* text=auto
|
||||
|
||||
*.gradle eol=lf diff=java
|
||||
*.java eol=lf diff=java
|
||||
*.kt eol=lf diff=java
|
||||
*.lua eol=lf
|
||||
*.md eol=lf diff=markdown
|
||||
*.txt eol=lf
|
||||
|
||||
*.png binary
|
||||
*.jar binary
|
||||
|
16
.github/ISSUE_TEMPLATE/bug_report.md
vendored
16
.github/ISSUE_TEMPLATE/bug_report.md
vendored
@@ -1,16 +0,0 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Report some misbehaviour in the mod
|
||||
labels: bug
|
||||
---
|
||||
|
||||
<!--
|
||||
## Before reporting
|
||||
- Search for the bug on the issue tracker. Make sure to look at closed issues too!
|
||||
-->
|
||||
|
||||
## Useful information to include:
|
||||
- Minecraft version
|
||||
- CC: Tweaked version
|
||||
- Logs: These will be located in the `logs/` directory of your Minecraft instance. Please upload them as a gist or directly into this editor.
|
||||
- Detailed reproduction steps: sometimes I can spot a bug pretty easily, but often it's much more obscure. The more information I have to help reproduce it, the quicker it'll get fixed.
|
33
.github/ISSUE_TEMPLATE/bug_report.yaml
vendored
Normal file
33
.github/ISSUE_TEMPLATE/bug_report.yaml
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
name: Bug report
|
||||
description: Report some misbehaviour in the mod
|
||||
labels: [ bug ]
|
||||
body:
|
||||
- type: dropdown
|
||||
id: mc-version
|
||||
attributes:
|
||||
label: Minecraft Version
|
||||
description: What version of Minecraft are you using?
|
||||
options:
|
||||
- 1.15.x
|
||||
- 1.16.x
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
id: version
|
||||
attributes:
|
||||
label: Version
|
||||
description: "What version of CC: Tweaked are you using?"
|
||||
placeholder: "e.g. 1.96.0"
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: details
|
||||
attributes:
|
||||
label: Details
|
||||
description: |
|
||||
Description of the bug. Please include the following:
|
||||
- Logs: These will be located in the `logs/` directory of your Minecraft
|
||||
instance. Please upload them as a gist or directly into this editor.
|
||||
- Detailed reproduction steps: sometimes I can spot a bug pretty easily,
|
||||
but often it's much more obscure. The more information I have to help
|
||||
reproduce it, the quicker it'll get fixed.
|
22
.gitpod.yml
Normal file
22
.gitpod.yml
Normal file
@@ -0,0 +1,22 @@
|
||||
image:
|
||||
file: config/gitpod/Dockerfile
|
||||
|
||||
ports:
|
||||
- port: 25565
|
||||
onOpen: notify
|
||||
|
||||
vscode:
|
||||
extensions:
|
||||
- eamodio.gitlens
|
||||
- github.vscode-pull-request-github
|
||||
- ms-azuretools.vscode-docker
|
||||
- redhat.java
|
||||
- richardwillis.vscode-gradle
|
||||
- vscjava.vscode-java-debug
|
||||
- vscode.github
|
||||
|
||||
tasks:
|
||||
- name: Setup pre-commit hool
|
||||
init: pre-commit install --config config/pre-commit/config.yml --allow-missing-config
|
||||
- name: Install npm packages
|
||||
init: npm ci
|
@@ -19,6 +19,10 @@ process. When building on Windows, Use `gradlew.bat` instead of `./gradlew`.
|
||||
- **Clone the repository:** `git clone https://github.com/SquidDev-CC/CC-Tweaked.git && cd CC-Tweaked`
|
||||
- **Setup Forge:** `./gradlew build`
|
||||
- **Run Minecraft:** `./gradlew runClient` (or run the `GradleStart` class from your IDE).
|
||||
- **Optionally:** For small PRs (especially those only touching Lua code), it may be easier to use GitPod, which
|
||||
provides a pre-configured environment: [](https://gitpod.io/#https://github.com/SquidDev-CC/CC-Tweaked/)
|
||||
|
||||
Do note you will need to download the mod after compiling to test.
|
||||
|
||||
If you want to run CC:T in a normal Minecraft instance, run `./gradlew build` and copy the `.jar` from `build/libs`.
|
||||
These commands may take a few minutes to run the first time, as the environment is set up, but should be much faster
|
||||
|
213
build.gradle
213
build.gradle
@@ -1,22 +1,14 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
maven {
|
||||
name = "forge"
|
||||
url = "https://maven.minecraftforge.net"
|
||||
}
|
||||
maven {
|
||||
name = "Sponge (Mixin)"
|
||||
url = "https://repo.spongepowered.org/repository/maven-public/"
|
||||
content { includeGroup "org.spongepowered" }
|
||||
}
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.google.code.gson:gson:2.8.1'
|
||||
classpath 'net.minecraftforge.gradle:ForgeGradle:4.1.9'
|
||||
classpath 'net.sf.proguard:proguard-gradle:6.1.0beta2'
|
||||
classpath 'org.spongepowered:mixingradle:0.7-SNAPSHOT'
|
||||
classpath 'net.minecraftforge.gradle:ForgeGradle:5.0.12'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,29 +16,44 @@ plugins {
|
||||
id "checkstyle"
|
||||
id "jacoco"
|
||||
id "maven-publish"
|
||||
id "com.github.hierynomus.license" version "0.15.0"
|
||||
id "com.github.hierynomus.license" version "0.16.1"
|
||||
id "com.matthewprenger.cursegradle" version "1.4.0"
|
||||
id "com.github.breadmoirai.github-release" version "2.2.12"
|
||||
id "org.jetbrains.kotlin.jvm" version "1.3.72"
|
||||
id "com.modrinth.minotaur" version "1.2.1"
|
||||
}
|
||||
|
||||
apply plugin: 'net.minecraftforge.gradle'
|
||||
apply plugin: 'org.spongepowered.mixin'
|
||||
|
||||
version = mod_version
|
||||
|
||||
group = "org.squiddev"
|
||||
archivesBaseName = "cc-tweaked-${mc_version}"
|
||||
|
||||
def javaVersion = JavaLanguageVersion.of(8)
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(8)
|
||||
languageVersion = javaVersion
|
||||
}
|
||||
|
||||
withSourcesJar()
|
||||
withJavadocJar()
|
||||
}
|
||||
|
||||
// Tragically java.toolchain.languageVersion doesn't apply to ForgeGradle's
|
||||
// tasks, so we force all launchers to use the right Java version.
|
||||
tasks.withType(JavaCompile).configureEach {
|
||||
javaCompiler = javaToolchains.compilerFor {
|
||||
languageVersion = javaVersion
|
||||
}
|
||||
}
|
||||
|
||||
tasks.withType(JavaExec).configureEach {
|
||||
javaLauncher = javaToolchains.launcherFor {
|
||||
languageVersion = javaVersion
|
||||
}
|
||||
}
|
||||
|
||||
minecraft {
|
||||
runs {
|
||||
client {
|
||||
@@ -90,9 +97,6 @@ minecraft {
|
||||
testServer {
|
||||
workingDirectory project.file('test-files/server')
|
||||
parent runs.server
|
||||
properties 'mixin.env.disableRefMap': 'true'
|
||||
|
||||
arg "-mixin.config=cctest.mixin.json"
|
||||
arg "--nogui"
|
||||
|
||||
mods {
|
||||
@@ -106,10 +110,7 @@ minecraft {
|
||||
mappings channel: 'official', version: mc_version
|
||||
|
||||
accessTransformer file('src/main/resources/META-INF/accesstransformer.cfg')
|
||||
}
|
||||
|
||||
mixin {
|
||||
add sourceSets.test, "cctest.refmap.json"
|
||||
accessTransformer file('src/test/resources/META-INF/accesstransformer.cfg')
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
@@ -137,10 +138,11 @@ dependencies {
|
||||
|
||||
minecraft "net.minecraftforge:forge:${mc_version}-${forge_version}"
|
||||
|
||||
compileOnly fg.deobf("mezz.jei:jei-1.16.4:7.6.0.58:api")
|
||||
compileOnly fg.deobf("com.blamejared.crafttweaker:CraftTweaker-1.16.4:7.0.0.63")
|
||||
compileOnly fg.deobf("mezz.jei:jei-1.16.5:7.7.0.104:api")
|
||||
compileOnly fg.deobf("com.blamejared.crafttweaker:CraftTweaker-1.16.5:7.1.0.313")
|
||||
compileOnly fg.deobf("commoble.morered:morered-1.16.5:2.1.1.0")
|
||||
|
||||
runtimeOnly fg.deobf("mezz.jei:jei-1.16.4:7.6.0.58")
|
||||
runtimeOnly fg.deobf("mezz.jei:jei-1.16.5:7.7.0.104")
|
||||
|
||||
shade 'org.squiddev:Cobalt:0.5.2-SNAPSHOT'
|
||||
|
||||
@@ -151,7 +153,6 @@ dependencies {
|
||||
testImplementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72'
|
||||
testImplementation 'org.jetbrains.kotlin:kotlin-reflect:1.3.72'
|
||||
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8'
|
||||
testAnnotationProcessor 'org.spongepowered:mixin:0.8.2:processor'
|
||||
|
||||
cctJavadoc 'cc.tweaked:cct-javadoc:1.4.0'
|
||||
}
|
||||
@@ -181,12 +182,12 @@ task luaJavadoc(type: Javadoc) {
|
||||
|
||||
jar {
|
||||
manifest {
|
||||
attributes(["Specification-Title": "computercraft",
|
||||
"Specification-Vendor": "SquidDev",
|
||||
"Specification-Version": "1",
|
||||
"Implementation-Title": "CC: Tweaked",
|
||||
"Implementation-Version": "${mod_version}",
|
||||
"Implementation-Vendor" :"SquidDev",
|
||||
attributes(["Specification-Title" : "computercraft",
|
||||
"Specification-Vendor" : "SquidDev",
|
||||
"Specification-Version" : "1",
|
||||
"Implementation-Title" : "CC: Tweaked",
|
||||
"Implementation-Version" : "${mod_version}",
|
||||
"Implementation-Vendor" : "SquidDev",
|
||||
"Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")])
|
||||
}
|
||||
|
||||
@@ -199,59 +200,6 @@ jar {
|
||||
}
|
||||
}
|
||||
|
||||
import java.nio.charset.StandardCharsets
|
||||
import java.nio.file.*
|
||||
import java.util.zip.*
|
||||
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.JsonElement
|
||||
import com.hierynomus.gradle.license.tasks.LicenseCheck
|
||||
import com.hierynomus.gradle.license.tasks.LicenseFormat
|
||||
import proguard.gradle.ProGuardTask
|
||||
|
||||
task proguard(type: ProGuardTask, dependsOn: jar) {
|
||||
description "Removes unused shadowed classes from the jar"
|
||||
group "compact"
|
||||
|
||||
injars jar.archivePath
|
||||
outjars "${jar.archivePath.absolutePath.replace(".jar", "")}-min.jar"
|
||||
|
||||
// Add the main runtime jar and all non-shadowed dependencies
|
||||
libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
|
||||
libraryjars "${System.getProperty('java.home')}/lib/jce.jar"
|
||||
doFirst {
|
||||
sourceSets.main.compileClasspath
|
||||
.filter { !it.name.contains("Cobalt") }
|
||||
.each { libraryjars it }
|
||||
}
|
||||
|
||||
// We want to avoid as much obfuscation as possible. We're only doing this to shrink code size.
|
||||
dontobfuscate; dontoptimize; keepattributes; keepparameternames
|
||||
|
||||
// Proguard will remove directories by default, but that breaks JarMount.
|
||||
keepdirectories 'data/computercraft/lua**'
|
||||
|
||||
// Preserve ComputerCraft classes - we only want to strip shadowed files.
|
||||
keep 'class dan200.computercraft.** { *; }'
|
||||
|
||||
// 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) {
|
||||
description "Replace the original jar with the minified version"
|
||||
group "compact"
|
||||
|
||||
doLast {
|
||||
Files.move(
|
||||
file("${jar.archivePath.absolutePath.replace(".jar", "")}-min.jar").toPath(),
|
||||
file(jar.archivePath).toPath(),
|
||||
StandardCopyOption.REPLACE_EXISTING
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
processResources {
|
||||
inputs.property "version", mod_version
|
||||
inputs.property "mcversion", mc_version
|
||||
@@ -265,18 +213,19 @@ processResources {
|
||||
["git", "-C", projectDir, "log", "--format=tformat:%an%n%cn"].execute().text.split('\n').each {
|
||||
if (!blacklist.contains(it)) contributors.add(it)
|
||||
}
|
||||
} catch(Exception e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
inputs.property "commithash", hash
|
||||
duplicatesStrategy = DuplicatesStrategy.INCLUDE
|
||||
|
||||
from(sourceSets.main.resources.srcDirs) {
|
||||
include 'META-INF/mods.toml'
|
||||
include 'data/computercraft/lua/rom/help/credits.txt'
|
||||
|
||||
expand 'version': mod_version,
|
||||
'mcversion': mc_version,
|
||||
'gitcontributors': contributors.sort(false, String.CASE_INSENSITIVE_ORDER).join('\n')
|
||||
'mcversion': mc_version,
|
||||
'gitcontributors': contributors.sort(false, String.CASE_INSENSITIVE_ORDER).join('\n')
|
||||
}
|
||||
|
||||
from(sourceSets.main.resources.srcDirs) {
|
||||
@@ -285,51 +234,15 @@ processResources {
|
||||
}
|
||||
}
|
||||
|
||||
task compressJson(dependsOn: jar) {
|
||||
group "compact"
|
||||
description "Minifies all JSON files, stripping whitespace"
|
||||
|
||||
def jarPath = file(jar.archivePath)
|
||||
|
||||
def tempPath = File.createTempFile("input", ".jar", temporaryDir)
|
||||
tempPath.deleteOnExit()
|
||||
|
||||
def gson = new GsonBuilder().create()
|
||||
|
||||
doLast {
|
||||
// Copy over all files in the current jar to the new one, running json files from GSON. As pretty printing
|
||||
// is turned off, they should be minified.
|
||||
new ZipFile(jarPath).withCloseable { inJar ->
|
||||
tempPath.getParentFile().mkdirs()
|
||||
new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(tempPath))).withCloseable { outJar ->
|
||||
inJar.entries().each { entry ->
|
||||
if(entry.directory) {
|
||||
outJar.putNextEntry(entry)
|
||||
} else if(!entry.name.endsWith(".json")) {
|
||||
outJar.putNextEntry(entry)
|
||||
inJar.getInputStream(entry).withCloseable { outJar << it }
|
||||
} else {
|
||||
ZipEntry newEntry = new ZipEntry(entry.name)
|
||||
newEntry.setTime(entry.time)
|
||||
outJar.putNextEntry(newEntry)
|
||||
|
||||
def element = inJar.getInputStream(entry).withCloseable { gson.fromJson(it.newReader("UTF8"), JsonElement.class) }
|
||||
outJar.write(gson.toJson(element).getBytes(StandardCharsets.UTF_8))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// And replace the original jar again
|
||||
Files.move(tempPath.toPath(), jarPath.toPath(), StandardCopyOption.REPLACE_EXISTING)
|
||||
}
|
||||
sourcesJar {
|
||||
duplicatesStrategy = DuplicatesStrategy.INCLUDE
|
||||
}
|
||||
|
||||
assemble.dependsOn compressJson
|
||||
|
||||
// Web tasks
|
||||
|
||||
|
||||
import com.hierynomus.gradle.license.tasks.LicenseCheck
|
||||
import com.hierynomus.gradle.license.tasks.LicenseFormat
|
||||
import org.apache.tools.ant.taskdefs.condition.Os
|
||||
|
||||
List<String> mkCommand(String command) {
|
||||
@@ -428,8 +341,8 @@ gradle.projectsEvaluated {
|
||||
}
|
||||
|
||||
|
||||
task licenseAPI(type: LicenseCheck);
|
||||
task licenseFormatAPI(type: LicenseFormat);
|
||||
task licenseAPI(type: LicenseCheck)
|
||||
task licenseFormatAPI(type: LicenseFormat)
|
||||
[licenseAPI, licenseFormatAPI].forEach {
|
||||
it.configure {
|
||||
source = sourceSets.main.java
|
||||
@@ -452,7 +365,7 @@ task setupServer(type: Copy) {
|
||||
tasks.register('testInGame', JavaExec.class).configure {
|
||||
it.group('test server')
|
||||
it.description("Runs tests on a temporary Minecraft server.")
|
||||
it.dependsOn(setupServer, 'prepareRunTestServer')
|
||||
it.dependsOn(setupServer, 'prepareRunTestServer', 'cleanTestInGame')
|
||||
|
||||
// Copy from runTestServer. We do it in this slightly odd way as runTestServer
|
||||
// isn't created until the task is configured (which is no good for us).
|
||||
@@ -506,31 +419,31 @@ task checkRelease {
|
||||
description "Verifies that everything is ready for a release"
|
||||
|
||||
inputs.property "version", mod_version
|
||||
inputs.file("src/main/resources/data/computercraft/lua/rom/help/changelog.txt")
|
||||
inputs.file("src/main/resources/data/computercraft/lua/rom/help/whatsnew.txt")
|
||||
inputs.file("src/main/resources/data/computercraft/lua/rom/help/changelog.md")
|
||||
inputs.file("src/main/resources/data/computercraft/lua/rom/help/whatsnew.md")
|
||||
|
||||
doLast {
|
||||
def ok = true
|
||||
|
||||
// Check we're targetting the current version
|
||||
def whatsnew = new File(projectDir, "src/main/resources/data/computercraft/lua/rom/help/whatsnew.txt").readLines()
|
||||
def whatsnew = new File(projectDir, "src/main/resources/data/computercraft/lua/rom/help/whatsnew.md").readLines()
|
||||
if (whatsnew[0] != "New features in CC: Tweaked $mod_version") {
|
||||
ok = false
|
||||
project.logger.error("Expected `whatsnew.txt' to target $mod_version.")
|
||||
project.logger.error("Expected `whatsnew.md' to target $mod_version.")
|
||||
}
|
||||
|
||||
// Check "read more" exists and trim it
|
||||
def idx = whatsnew.findIndexOf { it == 'Type "help changelog" to see the full version history.' }
|
||||
if (idx == -1) {
|
||||
ok = false
|
||||
project.logger.error("Must mention the changelog in whatsnew.txt")
|
||||
project.logger.error("Must mention the changelog in whatsnew.md")
|
||||
} else {
|
||||
whatsnew = whatsnew.getAt(0 ..< idx)
|
||||
whatsnew = whatsnew.getAt(0..<idx)
|
||||
}
|
||||
|
||||
// Check whatsnew and changelog match.
|
||||
def versionChangelog = "# " + whatsnew.join("\n")
|
||||
def changelog = new File(projectDir, "src/main/resources/data/computercraft/lua/rom/help/changelog.txt").getText()
|
||||
def changelog = new File(projectDir, "src/main/resources/data/computercraft/lua/rom/help/changelog.md").getText()
|
||||
if (!changelog.startsWith(versionChangelog)) {
|
||||
ok = false
|
||||
project.logger.error("whatsnew and changelog are not in sync")
|
||||
@@ -551,12 +464,24 @@ curseforge {
|
||||
relations {
|
||||
incompatible "computercraft"
|
||||
}
|
||||
|
||||
addGameVersion '1.16.4'
|
||||
addGameVersion '1.16.5'
|
||||
}
|
||||
}
|
||||
|
||||
import com.modrinth.minotaur.TaskModrinthUpload
|
||||
tasks.register('publishModrinth', TaskModrinthUpload.class).configure {
|
||||
dependsOn('assemble', 'reobfJar')
|
||||
onlyIf {
|
||||
project.hasProperty('modrinthApiKey')
|
||||
}
|
||||
|
||||
token = project.hasProperty('curseForgeApiKey')
|
||||
projectId = 'gu7yAYhd'
|
||||
versionNumber = project.mod_version
|
||||
uploadFile = jar
|
||||
addGameVersion(project.mc_version)
|
||||
addLoader('forge')
|
||||
}
|
||||
|
||||
tasks.withType(GenerateModuleMetadata) {
|
||||
// We can't generate metadata as that includes Forge as a dependency.
|
||||
enabled = false
|
||||
@@ -624,17 +549,17 @@ githubRelease {
|
||||
releaseName "[${mc_version}] ${mod_version}"
|
||||
body.set(project.provider({
|
||||
"## " + new File(projectDir, "src/main/resources/data/computercraft/lua/rom/help/whatsnew.txt")
|
||||
.readLines()
|
||||
.takeWhile { it != 'Type "help changelog" to see the full version history.' }
|
||||
.join("\n").trim()
|
||||
.readLines()
|
||||
.takeWhile { it != 'Type "help changelog" to see the full version history.' }
|
||||
.join("\n").trim()
|
||||
}))
|
||||
prerelease false
|
||||
}
|
||||
|
||||
def uploadTasks = ["publish", "curseforge", "githubRelease"]
|
||||
def uploadTasks = ["publish", "curseforge", "publishModrinth", "githubRelease"]
|
||||
uploadTasks.forEach { tasks.getByName(it).dependsOn checkRelease }
|
||||
|
||||
task uploadAll(dependsOn: uploadTasks) {
|
||||
group "upload"
|
||||
description "Uploads to all repositories (Maven, Curse, GitHub release)"
|
||||
description "Uploads to all repositories (Maven, Curse, Modrinth, GitHub release)"
|
||||
}
|
||||
|
@@ -7,9 +7,6 @@
|
||||
<suppress checks="StaticVariableName" files=".*[\\/]ComputerCraft.java" />
|
||||
<suppress checks="StaticVariableName" files=".*[\\/]ComputerCraftAPI.java" />
|
||||
|
||||
<!-- Do not check for missing package Javadoc. -->
|
||||
<suppress checks="JavadocStyle" files=".*[\\/]package-info.java" />
|
||||
|
||||
<!-- The commands API is documented in Lua. -->
|
||||
<suppress checks="SummaryJavadocCheck" files=".*[\\/]CommandAPI.java" />
|
||||
</suppressions>
|
||||
|
8
config/gitpod/Dockerfile
Normal file
8
config/gitpod/Dockerfile
Normal file
@@ -0,0 +1,8 @@
|
||||
FROM gitpod/workspace-base
|
||||
|
||||
USER gitpod
|
||||
|
||||
RUN sudo apt-get -q update \
|
||||
&& sudo apt-get install -yq openjdk-8-jdk openjdk-16-jdk python3-pip npm \
|
||||
&& sudo pip3 install pre-commit \
|
||||
&& sudo update-java-alternatives --set java-1.8.0-openjdk-amd64
|
@@ -1,6 +1,7 @@
|
||||
# Mod properties
|
||||
mod_version=1.96.0
|
||||
mod_version=1.97.0
|
||||
|
||||
# Minecraft properties (update mods.toml when changing)
|
||||
mc_version=1.16.4
|
||||
forge_version=35.1.16
|
||||
mc_version=1.16.5
|
||||
forge_version=36.1.0
|
||||
# NO SERIOUSLY, UPDATE mods.toml WHEN CHANGING
|
||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,5 +1,5 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
@@ -36,7 +36,6 @@ public final class ComputerCraft
|
||||
public static int maximumFilesOpen = 128;
|
||||
public static boolean disableLua51Features = false;
|
||||
public static String defaultComputerSettings = "";
|
||||
public static boolean debugEnable = true;
|
||||
public static boolean logComputerErrors = true;
|
||||
public static boolean commandRequireCreative = true;
|
||||
|
||||
|
@@ -32,7 +32,7 @@ public final class TransformedModel
|
||||
public TransformedModel( @Nonnull IBakedModel model )
|
||||
{
|
||||
this.model = Objects.requireNonNull( model );
|
||||
this.matrix = TransformationMatrix.identity();
|
||||
matrix = TransformationMatrix.identity();
|
||||
}
|
||||
|
||||
public static TransformedModel of( @Nonnull ModelResourceLocation location )
|
||||
|
@@ -30,7 +30,7 @@ public class FileOperationException extends IOException
|
||||
public FileOperationException( @Nonnull String message )
|
||||
{
|
||||
super( Objects.requireNonNull( message, "message cannot be null" ) );
|
||||
this.filename = null;
|
||||
filename = null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
@@ -19,14 +19,14 @@ public class LuaException extends Exception
|
||||
public LuaException( @Nullable String message )
|
||||
{
|
||||
super( message );
|
||||
this.hasLevel = false;
|
||||
this.level = 1;
|
||||
hasLevel = false;
|
||||
level = 1;
|
||||
}
|
||||
|
||||
public LuaException( @Nullable String message, int level )
|
||||
{
|
||||
super( message );
|
||||
this.hasLevel = true;
|
||||
hasLevel = true;
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
|
@@ -30,14 +30,14 @@ public final class MethodResult
|
||||
|
||||
private MethodResult( Object[] arguments, ILuaCallback callback )
|
||||
{
|
||||
this.result = arguments;
|
||||
result = arguments;
|
||||
this.callback = callback;
|
||||
this.adjust = 0;
|
||||
adjust = 0;
|
||||
}
|
||||
|
||||
private MethodResult( Object[] arguments, ILuaCallback callback, int adjust )
|
||||
{
|
||||
this.result = arguments;
|
||||
result = arguments;
|
||||
this.callback = callback;
|
||||
this.adjust = adjust;
|
||||
}
|
||||
|
40
src/main/java/dan200/computercraft/client/ClientHooks.java
Normal file
40
src/main/java/dan200/computercraft/client/ClientHooks.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.client;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.peripheral.monitor.ClientMonitor;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
|
||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Dist.CLIENT )
|
||||
public class ClientHooks
|
||||
{
|
||||
@SubscribeEvent
|
||||
public static void onWorldUnload( WorldEvent.Unload event )
|
||||
{
|
||||
if( event.getWorld().isClientSide() )
|
||||
{
|
||||
ClientMonitor.destroyAll();
|
||||
SoundManager.reset();
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onLogIn( ClientPlayerNetworkEvent.LoggedInEvent event )
|
||||
{
|
||||
ComputerCraft.clientComputerRegistry.reset();
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onLogOut( ClientPlayerNetworkEvent.LoggedOutEvent event )
|
||||
{
|
||||
ComputerCraft.clientComputerRegistry.reset();
|
||||
}
|
||||
}
|
@@ -6,31 +6,39 @@
|
||||
package dan200.computercraft.client;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.client.gui.*;
|
||||
import dan200.computercraft.client.render.TileEntityMonitorRenderer;
|
||||
import dan200.computercraft.client.render.TileEntityTurtleRenderer;
|
||||
import dan200.computercraft.client.render.TurtleModelLoader;
|
||||
import dan200.computercraft.client.render.TurtlePlayerRenderer;
|
||||
import dan200.computercraft.shared.Registry;
|
||||
import dan200.computercraft.shared.common.IColouredItem;
|
||||
import dan200.computercraft.shared.computer.inventory.ContainerComputer;
|
||||
import dan200.computercraft.shared.computer.inventory.ContainerViewComputer;
|
||||
import dan200.computercraft.shared.media.items.ItemDisk;
|
||||
import dan200.computercraft.shared.media.items.ItemTreasureDisk;
|
||||
import dan200.computercraft.shared.pocket.inventory.ContainerPocketComputer;
|
||||
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
|
||||
import dan200.computercraft.shared.util.Colour;
|
||||
import net.minecraft.client.renderer.model.IBakedModel;
|
||||
import net.minecraft.client.renderer.model.IUnbakedModel;
|
||||
import net.minecraft.client.gui.ScreenManager;
|
||||
import net.minecraft.client.renderer.RenderType;
|
||||
import net.minecraft.client.renderer.RenderTypeLookup;
|
||||
import net.minecraft.client.renderer.model.ModelResourceLocation;
|
||||
import net.minecraft.inventory.container.PlayerContainer;
|
||||
import net.minecraft.item.IItemPropertyGetter;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemModelsProperties;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.client.event.ColorHandlerEvent;
|
||||
import net.minecraftforge.client.event.ModelBakeEvent;
|
||||
import net.minecraftforge.client.event.ModelRegistryEvent;
|
||||
import net.minecraftforge.client.event.TextureStitchEvent;
|
||||
import net.minecraftforge.client.model.ModelLoader;
|
||||
import net.minecraftforge.client.model.ModelLoaderRegistry;
|
||||
import net.minecraftforge.client.model.SimpleModelTransform;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.client.registry.RenderingRegistry;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Registers textures and models for items.
|
||||
@@ -39,6 +47,7 @@ import java.util.Map;
|
||||
public final class ClientRegistry
|
||||
{
|
||||
private static final String[] EXTRA_MODELS = new String[] {
|
||||
// Turtle upgrades
|
||||
"turtle_modem_normal_off_left",
|
||||
"turtle_modem_normal_on_left",
|
||||
"turtle_modem_normal_off_right",
|
||||
@@ -54,56 +63,20 @@ public final class ClientRegistry
|
||||
"turtle_speaker_upgrade_left",
|
||||
"turtle_speaker_upgrade_right",
|
||||
|
||||
// Turtle block renderer
|
||||
"turtle_colour",
|
||||
"turtle_elf_overlay",
|
||||
};
|
||||
|
||||
private static final String[] EXTRA_TEXTURES = new String[] {
|
||||
// TODO: Gather these automatically from the model. Sadly the model loader isn't available
|
||||
// when stitching textures.
|
||||
"block/turtle_colour",
|
||||
"block/turtle_elf_overlay",
|
||||
"block/turtle_crafty_face",
|
||||
"block/turtle_speaker_face",
|
||||
};
|
||||
|
||||
private ClientRegistry() {}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void registerModels( ModelRegistryEvent event )
|
||||
{
|
||||
ModelLoaderRegistry.registerLoader( new ResourceLocation( ComputerCraft.MOD_ID, "turtle" ), TurtleModelLoader.INSTANCE );
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onTextureStitchEvent( TextureStitchEvent.Pre event )
|
||||
{
|
||||
if( !event.getMap().location().equals( PlayerContainer.BLOCK_ATLAS ) ) return;
|
||||
|
||||
for( String extra : EXTRA_TEXTURES )
|
||||
for( String model : EXTRA_MODELS )
|
||||
{
|
||||
event.addSprite( new ResourceLocation( ComputerCraft.MOD_ID, extra ) );
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onModelBakeEvent( ModelBakeEvent event )
|
||||
{
|
||||
// Load all extra models
|
||||
ModelLoader loader = event.getModelLoader();
|
||||
Map<ResourceLocation, IBakedModel> registry = event.getModelRegistry();
|
||||
|
||||
for( String modelName : EXTRA_MODELS )
|
||||
{
|
||||
ResourceLocation location = new ResourceLocation( ComputerCraft.MOD_ID, "item/" + modelName );
|
||||
IUnbakedModel model = loader.getModel( location );
|
||||
model.getMaterials( loader::getModel, new HashSet<>() );
|
||||
|
||||
IBakedModel baked = model.bake( loader, ModelLoader.defaultTextureGetter(), SimpleModelTransform.IDENTITY, location );
|
||||
if( baked != null )
|
||||
{
|
||||
registry.put( new ModelResourceLocation( new ResourceLocation( ComputerCraft.MOD_ID, modelName ), "inventory" ), baked );
|
||||
}
|
||||
ModelLoader.addSpecialModel( new ModelResourceLocation( new ResourceLocation( ComputerCraft.MOD_ID, model ), "inventory" ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,4 +121,61 @@ public final class ClientRegistry
|
||||
Registry.ModBlocks.TURTLE_NORMAL.get(), Registry.ModBlocks.TURTLE_ADVANCED.get()
|
||||
);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void setupClient( FMLClientSetupEvent event )
|
||||
{
|
||||
registerContainers();
|
||||
|
||||
// While turtles themselves are not transparent, their upgrades may be.
|
||||
RenderTypeLookup.setRenderLayer( Registry.ModBlocks.TURTLE_NORMAL.get(), RenderType.translucent() );
|
||||
RenderTypeLookup.setRenderLayer( Registry.ModBlocks.TURTLE_ADVANCED.get(), RenderType.translucent() );
|
||||
|
||||
// Monitors' textures have transparent fronts and so count as cutouts.
|
||||
RenderTypeLookup.setRenderLayer( Registry.ModBlocks.MONITOR_NORMAL.get(), RenderType.cutout() );
|
||||
RenderTypeLookup.setRenderLayer( Registry.ModBlocks.MONITOR_ADVANCED.get(), RenderType.cutout() );
|
||||
|
||||
// Setup TESRs
|
||||
net.minecraftforge.fml.client.registry.ClientRegistry.bindTileEntityRenderer( Registry.ModTiles.MONITOR_NORMAL.get(), TileEntityMonitorRenderer::new );
|
||||
net.minecraftforge.fml.client.registry.ClientRegistry.bindTileEntityRenderer( Registry.ModTiles.MONITOR_ADVANCED.get(), TileEntityMonitorRenderer::new );
|
||||
net.minecraftforge.fml.client.registry.ClientRegistry.bindTileEntityRenderer( Registry.ModTiles.TURTLE_NORMAL.get(), TileEntityTurtleRenderer::new );
|
||||
net.minecraftforge.fml.client.registry.ClientRegistry.bindTileEntityRenderer( Registry.ModTiles.TURTLE_ADVANCED.get(), TileEntityTurtleRenderer::new );
|
||||
|
||||
RenderingRegistry.registerEntityRenderingHandler( Registry.ModEntities.TURTLE_PLAYER.get(), TurtlePlayerRenderer::new );
|
||||
|
||||
registerItemProperty( "state",
|
||||
( stack, world, player ) -> ItemPocketComputer.getState( stack ).ordinal(),
|
||||
Registry.ModItems.POCKET_COMPUTER_NORMAL, Registry.ModItems.POCKET_COMPUTER_ADVANCED
|
||||
);
|
||||
registerItemProperty( "coloured",
|
||||
( stack, world, player ) -> IColouredItem.getColourBasic( stack ) != -1 ? 1 : 0,
|
||||
Registry.ModItems.POCKET_COMPUTER_NORMAL, Registry.ModItems.POCKET_COMPUTER_ADVANCED
|
||||
);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
private static void registerItemProperty( String name, IItemPropertyGetter getter, Supplier<? extends Item>... items )
|
||||
{
|
||||
ResourceLocation id = new ResourceLocation( ComputerCraft.MOD_ID, name );
|
||||
for( Supplier<? extends Item> item : items )
|
||||
{
|
||||
ItemModelsProperties.register( item.get(), id, getter );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void registerContainers()
|
||||
{
|
||||
// My IDE doesn't think so, but we do actually need these generics.
|
||||
|
||||
ScreenManager.<ContainerComputer, GuiComputer<ContainerComputer>>register( Registry.ModContainers.COMPUTER.get(), GuiComputer::create );
|
||||
ScreenManager.<ContainerPocketComputer, GuiComputer<ContainerPocketComputer>>register( Registry.ModContainers.POCKET_COMPUTER.get(), GuiComputer::createPocket );
|
||||
ScreenManager.register( Registry.ModContainers.TURTLE.get(), GuiTurtle::new );
|
||||
|
||||
ScreenManager.register( Registry.ModContainers.PRINTER.get(), GuiPrinter::new );
|
||||
ScreenManager.register( Registry.ModContainers.DISK_DRIVE.get(), GuiDiskDrive::new );
|
||||
ScreenManager.register( Registry.ModContainers.PRINTOUT.get(), GuiPrintout::new );
|
||||
|
||||
ScreenManager.<ContainerViewComputer, GuiComputer<ContainerViewComputer>>register( Registry.ModContainers.VIEW_COMPUTER.get(), GuiComputer::createView );
|
||||
}
|
||||
}
|
||||
|
84
src/main/java/dan200/computercraft/client/SoundManager.java
Normal file
84
src/main/java/dan200/computercraft/client/SoundManager.java
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.client;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.audio.ISound;
|
||||
import net.minecraft.client.audio.ITickableSound;
|
||||
import net.minecraft.client.audio.LocatableSound;
|
||||
import net.minecraft.client.audio.SoundHandler;
|
||||
import net.minecraft.util.SoundCategory;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.util.math.vector.Vector3d;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SoundManager
|
||||
{
|
||||
private static final Map<UUID, MoveableSound> sounds = new HashMap<>();
|
||||
|
||||
public static void playSound( UUID source, Vector3d position, SoundEvent event, float volume, float pitch )
|
||||
{
|
||||
SoundHandler soundManager = Minecraft.getInstance().getSoundManager();
|
||||
|
||||
MoveableSound oldSound = sounds.get( source );
|
||||
if( oldSound != null ) soundManager.stop( oldSound );
|
||||
|
||||
MoveableSound newSound = new MoveableSound( event, position, volume, pitch );
|
||||
sounds.put( source, newSound );
|
||||
soundManager.play( newSound );
|
||||
}
|
||||
|
||||
public static void stopSound( UUID source )
|
||||
{
|
||||
ISound sound = sounds.remove( source );
|
||||
if( sound == null ) return;
|
||||
|
||||
Minecraft.getInstance().getSoundManager().stop( sound );
|
||||
}
|
||||
|
||||
public static void moveSound( UUID source, Vector3d position )
|
||||
{
|
||||
MoveableSound sound = sounds.get( source );
|
||||
if( sound != null ) sound.setPosition( position );
|
||||
}
|
||||
|
||||
public static void reset()
|
||||
{
|
||||
sounds.clear();
|
||||
}
|
||||
|
||||
private static class MoveableSound extends LocatableSound implements ITickableSound
|
||||
{
|
||||
protected MoveableSound( SoundEvent sound, Vector3d position, float volume, float pitch )
|
||||
{
|
||||
super( sound, SoundCategory.RECORDS );
|
||||
setPosition( position );
|
||||
this.volume = volume;
|
||||
this.pitch = pitch;
|
||||
}
|
||||
|
||||
void setPosition( Vector3d position )
|
||||
{
|
||||
x = (float) position.x();
|
||||
y = (float) position.y();
|
||||
z = (float) position.z();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStopped()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,208 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.client.gui;
|
||||
|
||||
import com.mojang.blaze3d.matrix.MatrixStack;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.client.gui.widgets.ComputerSidebar;
|
||||
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
|
||||
import dan200.computercraft.shared.computer.core.ClientComputer;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.inventory.ContainerComputerBase;
|
||||
import dan200.computercraft.shared.computer.upload.FileUpload;
|
||||
import dan200.computercraft.shared.computer.upload.UploadResult;
|
||||
import dan200.computercraft.shared.network.NetworkHandler;
|
||||
import dan200.computercraft.shared.network.server.ContinueUploadMessage;
|
||||
import dan200.computercraft.shared.network.server.UploadFileMessage;
|
||||
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SeekableByteChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class ComputerScreenBase<T extends ContainerComputerBase> extends ContainerScreen<T>
|
||||
{
|
||||
private static final ITextComponent OK = new TranslationTextComponent( "gui.ok" );
|
||||
private static final ITextComponent CANCEL = new TranslationTextComponent( "gui.cancel" );
|
||||
private static final ITextComponent OVERWRITE = new TranslationTextComponent( "gui.computercraft.upload.overwrite_button" );
|
||||
|
||||
protected WidgetTerminal terminal;
|
||||
protected final ClientComputer computer;
|
||||
protected final ComputerFamily family;
|
||||
|
||||
protected final int sidebarYOffset;
|
||||
|
||||
public ComputerScreenBase( T container, PlayerInventory player, ITextComponent title, int sidebarYOffset )
|
||||
{
|
||||
super( container, player, title );
|
||||
computer = (ClientComputer) container.getComputer();
|
||||
family = container.getFamily();
|
||||
this.sidebarYOffset = sidebarYOffset;
|
||||
}
|
||||
|
||||
protected abstract WidgetTerminal createTerminal();
|
||||
|
||||
@Override
|
||||
protected final void init()
|
||||
{
|
||||
super.init();
|
||||
minecraft.keyboardHandler.setSendRepeatsToGui( true );
|
||||
|
||||
terminal = addButton( createTerminal() );
|
||||
ComputerSidebar.addButtons( this, computer, this::addButton, leftPos, topPos + sidebarYOffset );
|
||||
setFocused( terminal );
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void removed()
|
||||
{
|
||||
super.removed();
|
||||
minecraft.keyboardHandler.setSendRepeatsToGui( false );
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void tick()
|
||||
{
|
||||
super.tick();
|
||||
terminal.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean keyPressed( int key, int scancode, int modifiers )
|
||||
{
|
||||
// Forward the tab key to the terminal, rather than moving between controls.
|
||||
if( key == GLFW.GLFW_KEY_TAB && getFocused() != null && getFocused() == terminal )
|
||||
{
|
||||
return getFocused().keyPressed( key, scancode, modifiers );
|
||||
}
|
||||
|
||||
return super.keyPressed( key, scancode, modifiers );
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final void render( @Nonnull MatrixStack stack, int mouseX, int mouseY, float partialTicks )
|
||||
{
|
||||
renderBackground( stack );
|
||||
super.render( stack, mouseX, mouseY, partialTicks );
|
||||
renderTooltip( stack, mouseX, mouseY );
|
||||
}
|
||||
|
||||
@Override
|
||||
public final 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
|
||||
protected void renderLabels( @Nonnull MatrixStack transform, int mouseX, int mouseY )
|
||||
{
|
||||
// Skip rendering labels.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFilesDrop( @Nonnull List<Path> files )
|
||||
{
|
||||
if( files.isEmpty() ) return;
|
||||
|
||||
if( computer == null || !computer.isOn() )
|
||||
{
|
||||
alert( UploadResult.FAILED_TITLE, UploadResult.COMPUTER_OFF_MSG );
|
||||
return;
|
||||
}
|
||||
|
||||
long size = 0;
|
||||
|
||||
List<FileUpload> toUpload = new ArrayList<>();
|
||||
for( Path file : files )
|
||||
{
|
||||
// TODO: Recurse directories? If so, we probably want to shunt this off-thread.
|
||||
if( !Files.isRegularFile( file ) ) continue;
|
||||
|
||||
try( SeekableByteChannel sbc = Files.newByteChannel( file ) )
|
||||
{
|
||||
long fileSize = sbc.size();
|
||||
if( fileSize > UploadFileMessage.MAX_SIZE || (size += fileSize) >= UploadFileMessage.MAX_SIZE )
|
||||
{
|
||||
alert( UploadResult.FAILED_TITLE, UploadResult.TOO_MUCH_MSG );
|
||||
return;
|
||||
}
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocateDirect( (int) fileSize );
|
||||
sbc.read( buffer );
|
||||
buffer.flip();
|
||||
|
||||
toUpload.add( new FileUpload( file.getFileName().toString(), buffer ) );
|
||||
}
|
||||
catch( IOException e )
|
||||
{
|
||||
ComputerCraft.log.error( "Failed uploading files", e );
|
||||
alert( UploadResult.FAILED_TITLE, new TranslationTextComponent( "computercraft.gui.upload.failed.generic", e.getMessage() ) );
|
||||
}
|
||||
}
|
||||
|
||||
if( toUpload.size() > 0 )
|
||||
{
|
||||
NetworkHandler.sendToServer( new UploadFileMessage( computer.getInstanceID(), toUpload ) );
|
||||
}
|
||||
}
|
||||
|
||||
public void uploadResult( UploadResult result, ITextComponent message )
|
||||
{
|
||||
switch( result )
|
||||
{
|
||||
case SUCCESS:
|
||||
alert( UploadResult.SUCCESS_TITLE, message );
|
||||
break;
|
||||
case ERROR:
|
||||
alert( UploadResult.FAILED_TITLE, message );
|
||||
break;
|
||||
case CONFIRM_OVERWRITE:
|
||||
OptionScreen.show(
|
||||
minecraft, UploadResult.UPLOAD_OVERWRITE, message,
|
||||
Arrays.asList(
|
||||
OptionScreen.newButton( CANCEL, b -> cancelUpload() ),
|
||||
OptionScreen.newButton( OVERWRITE, b -> continueUpload() )
|
||||
),
|
||||
this::cancelUpload
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void continueUpload()
|
||||
{
|
||||
if( minecraft.screen instanceof OptionScreen ) ((OptionScreen) minecraft.screen).disable();
|
||||
NetworkHandler.sendToServer( new ContinueUploadMessage( computer.getInstanceID(), true ) );
|
||||
}
|
||||
|
||||
private void cancelUpload()
|
||||
{
|
||||
minecraft.setScreen( this );
|
||||
NetworkHandler.sendToServer( new ContinueUploadMessage( computer.getInstanceID(), false ) );
|
||||
}
|
||||
|
||||
private void alert( ITextComponent title, ITextComponent message )
|
||||
{
|
||||
OptionScreen.show( minecraft, title, message,
|
||||
Collections.singletonList( OptionScreen.newButton( OK, b -> minecraft.setScreen( this ) ) ),
|
||||
() -> minecraft.setScreen( this )
|
||||
);
|
||||
}
|
||||
}
|
@@ -8,45 +8,35 @@ package dan200.computercraft.client.gui;
|
||||
import com.mojang.blaze3d.matrix.MatrixStack;
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.client.gui.widgets.ComputerSidebar;
|
||||
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
|
||||
import dan200.computercraft.client.gui.widgets.WidgetWrapper;
|
||||
import dan200.computercraft.client.render.ComputerBorderRenderer;
|
||||
import dan200.computercraft.shared.computer.core.ClientComputer;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.inventory.ContainerComputer;
|
||||
import dan200.computercraft.shared.computer.inventory.ContainerComputerBase;
|
||||
import dan200.computercraft.shared.computer.inventory.ContainerViewComputer;
|
||||
import dan200.computercraft.shared.pocket.inventory.ContainerPocketComputer;
|
||||
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import static dan200.computercraft.client.render.ComputerBorderRenderer.BORDER;
|
||||
import static dan200.computercraft.client.render.ComputerBorderRenderer.MARGIN;
|
||||
|
||||
public final class GuiComputer<T extends ContainerComputerBase> extends ContainerScreen<T>
|
||||
public final class GuiComputer<T extends ContainerComputerBase> extends ComputerScreenBase<T>
|
||||
{
|
||||
private final ComputerFamily family;
|
||||
private final ClientComputer computer;
|
||||
private final int termWidth;
|
||||
private final int termHeight;
|
||||
|
||||
private WidgetTerminal terminal;
|
||||
private WidgetWrapper terminalWrapper;
|
||||
|
||||
private GuiComputer(
|
||||
T container, PlayerInventory player, ITextComponent title, int termWidth, int termHeight
|
||||
)
|
||||
{
|
||||
super( container, player, title );
|
||||
family = container.getFamily();
|
||||
computer = (ClientComputer) container.getComputer();
|
||||
super( container, player, title, BORDER );
|
||||
this.termWidth = termWidth;
|
||||
this.termHeight = termHeight;
|
||||
terminal = null;
|
||||
|
||||
imageWidth = WidgetTerminal.getWidth( termWidth ) + BORDER * 2 + ComputerSidebar.WIDTH;
|
||||
imageHeight = WidgetTerminal.getHeight( termHeight ) + BORDER * 2;
|
||||
}
|
||||
|
||||
public static GuiComputer<ContainerComputer> create( ContainerComputer container, PlayerInventory inventory, ITextComponent component )
|
||||
@@ -73,87 +63,21 @@ public final class GuiComputer<T extends ContainerComputerBase> extends Containe
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void init()
|
||||
protected WidgetTerminal createTerminal()
|
||||
{
|
||||
minecraft.keyboardHandler.setSendRepeatsToGui( true );
|
||||
|
||||
int termPxWidth = termWidth * FixedWidthFontRenderer.FONT_WIDTH;
|
||||
int termPxHeight = termHeight * FixedWidthFontRenderer.FONT_HEIGHT;
|
||||
|
||||
imageWidth = termPxWidth + MARGIN * 2 + BORDER * 2;
|
||||
imageHeight = termPxHeight + MARGIN * 2 + BORDER * 2;
|
||||
|
||||
super.init();
|
||||
|
||||
terminal = new WidgetTerminal( minecraft, () -> computer, termWidth, termHeight, MARGIN, MARGIN, MARGIN, MARGIN );
|
||||
terminalWrapper = new WidgetWrapper( terminal, MARGIN + BORDER + leftPos, MARGIN + BORDER + topPos, termPxWidth, termPxHeight );
|
||||
|
||||
children.add( terminalWrapper );
|
||||
setFocused( terminalWrapper );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removed()
|
||||
{
|
||||
super.removed();
|
||||
children.remove( terminal );
|
||||
terminal = null;
|
||||
minecraft.keyboardHandler.setSendRepeatsToGui( false );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick()
|
||||
{
|
||||
super.tick();
|
||||
terminal.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyPressed( int key, int scancode, int modifiers )
|
||||
{
|
||||
// Forward the tab key to the terminal, rather than moving between controls.
|
||||
if( key == GLFW.GLFW_KEY_TAB && getFocused() != null && getFocused() == terminalWrapper )
|
||||
{
|
||||
return getFocused().keyPressed( key, scancode, modifiers );
|
||||
}
|
||||
|
||||
return super.keyPressed( key, scancode, modifiers );
|
||||
return new WidgetTerminal( computer,
|
||||
leftPos + ComputerSidebar.WIDTH + BORDER, topPos + BORDER, termWidth, termHeight
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderBg( @Nonnull MatrixStack stack, float partialTicks, int mouseX, int mouseY )
|
||||
{
|
||||
// Draw terminal
|
||||
terminal.draw( terminalWrapper.getX(), terminalWrapper.getY() );
|
||||
|
||||
// Draw a border around the terminal
|
||||
RenderSystem.color4f( 1, 1, 1, 1 );
|
||||
minecraft.getTextureManager().bind( ComputerBorderRenderer.getTexture( family ) );
|
||||
ComputerBorderRenderer.render(
|
||||
terminalWrapper.getX() - MARGIN, terminalWrapper.getY() - MARGIN, getBlitOffset(),
|
||||
terminalWrapper.getWidth() + MARGIN * 2, terminalWrapper.getHeight() + MARGIN * 2
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render( @Nonnull MatrixStack stack, int mouseX, int mouseY, float partialTicks )
|
||||
{
|
||||
super.render( stack, mouseX, mouseY, partialTicks );
|
||||
renderTooltip( stack, 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
|
||||
protected void renderLabels( @Nonnull MatrixStack transform, int mouseX, int mouseY )
|
||||
{
|
||||
// Skip rendering labels.
|
||||
ComputerBorderRenderer.render( terminal.x, terminal.y, getBlitOffset(), terminal.getWidth(), terminal.getHeight() );
|
||||
ComputerSidebar.renderBackground( stack, leftPos, topPos + sidebarYOffset );
|
||||
}
|
||||
}
|
||||
|
@@ -24,14 +24,6 @@ public class GuiPrinter extends ContainerScreen<ContainerPrinter>
|
||||
super( container, player, title );
|
||||
}
|
||||
|
||||
/*@Override
|
||||
protected void drawGuiContainerForegroundLayer( int mouseX, int mouseY )
|
||||
{
|
||||
String title = getTitle().getFormattedText();
|
||||
font.drawString( title, (xSize - font.getStringWidth( title )) / 2.0f, 6, 0x404040 );
|
||||
font.drawString( I18n.format( "container.inventory" ), 8, ySize - 96 + 2, 0x404040 );
|
||||
}*/
|
||||
|
||||
@Override
|
||||
protected void renderBg( @Nonnull MatrixStack transform, float partialTicks, int mouseX, int mouseY )
|
||||
{
|
||||
|
@@ -8,137 +8,67 @@ package dan200.computercraft.client.gui;
|
||||
import com.mojang.blaze3d.matrix.MatrixStack;
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.client.gui.widgets.ComputerSidebar;
|
||||
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
|
||||
import dan200.computercraft.client.gui.widgets.WidgetWrapper;
|
||||
import dan200.computercraft.shared.computer.core.ClientComputer;
|
||||
import dan200.computercraft.client.render.ComputerBorderRenderer;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.turtle.inventory.ContainerTurtle;
|
||||
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class GuiTurtle extends ContainerScreen<ContainerTurtle>
|
||||
import static dan200.computercraft.shared.turtle.inventory.ContainerTurtle.*;
|
||||
|
||||
public class GuiTurtle extends ComputerScreenBase<ContainerTurtle>
|
||||
{
|
||||
private static final ResourceLocation BACKGROUND_NORMAL = new ResourceLocation( ComputerCraft.MOD_ID, "textures/gui/turtle_normal.png" );
|
||||
private static final ResourceLocation BACKGROUND_ADVANCED = new ResourceLocation( ComputerCraft.MOD_ID, "textures/gui/turtle_advanced.png" );
|
||||
|
||||
private final ContainerTurtle container;
|
||||
private static final int TEX_WIDTH = 254;
|
||||
private static final int TEX_HEIGHT = 217;
|
||||
|
||||
private final ComputerFamily family;
|
||||
private final ClientComputer computer;
|
||||
|
||||
private WidgetTerminal terminal;
|
||||
private WidgetWrapper terminalWrapper;
|
||||
|
||||
public GuiTurtle( ContainerTurtle container, PlayerInventory player, ITextComponent title )
|
||||
{
|
||||
super( container, player, title );
|
||||
|
||||
this.container = container;
|
||||
super( container, player, title, BORDER );
|
||||
family = container.getFamily();
|
||||
computer = (ClientComputer) container.getComputer();
|
||||
|
||||
imageWidth = 254;
|
||||
imageHeight = 217;
|
||||
imageWidth = TEX_WIDTH + ComputerSidebar.WIDTH;
|
||||
imageHeight = TEX_HEIGHT;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init()
|
||||
protected WidgetTerminal createTerminal()
|
||||
{
|
||||
super.init();
|
||||
minecraft.keyboardHandler.setSendRepeatsToGui( true );
|
||||
|
||||
int termPxWidth = ComputerCraft.turtleTermWidth * FixedWidthFontRenderer.FONT_WIDTH;
|
||||
int termPxHeight = ComputerCraft.turtleTermHeight * FixedWidthFontRenderer.FONT_HEIGHT;
|
||||
|
||||
terminal = new WidgetTerminal(
|
||||
minecraft, () -> computer,
|
||||
ComputerCraft.turtleTermWidth,
|
||||
ComputerCraft.turtleTermHeight,
|
||||
2, 2, 2, 2
|
||||
return new WidgetTerminal(
|
||||
computer, leftPos + BORDER + ComputerSidebar.WIDTH, topPos + BORDER,
|
||||
ComputerCraft.turtleTermWidth, ComputerCraft.turtleTermHeight
|
||||
);
|
||||
terminalWrapper = new WidgetWrapper( terminal, 2 + 8 + leftPos, 2 + 8 + topPos, termPxWidth, termPxHeight );
|
||||
|
||||
children.add( terminalWrapper );
|
||||
setFocused( terminalWrapper );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removed()
|
||||
{
|
||||
super.removed();
|
||||
children.remove( terminal );
|
||||
terminal = null;
|
||||
minecraft.keyboardHandler.setSendRepeatsToGui( false );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick()
|
||||
{
|
||||
super.tick();
|
||||
terminal.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyPressed( int key, int scancode, int modifiers )
|
||||
{
|
||||
// Forward the tab key to the terminal, rather than moving between controls.
|
||||
if( key == GLFW.GLFW_KEY_TAB && getFocused() != null && getFocused() == terminalWrapper )
|
||||
{
|
||||
return getFocused().keyPressed( key, scancode, modifiers );
|
||||
}
|
||||
|
||||
return super.keyPressed( key, scancode, modifiers );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void renderBg( @Nonnull MatrixStack transform, float partialTicks, int mouseX, int mouseY )
|
||||
{
|
||||
// Draw term
|
||||
ResourceLocation texture = family == ComputerFamily.ADVANCED ? BACKGROUND_ADVANCED : BACKGROUND_NORMAL;
|
||||
terminal.draw( terminalWrapper.getX(), terminalWrapper.getY() );
|
||||
boolean advanced = family == ComputerFamily.ADVANCED;
|
||||
minecraft.getTextureManager().bind( advanced ? BACKGROUND_ADVANCED : BACKGROUND_NORMAL );
|
||||
blit( transform, leftPos + ComputerSidebar.WIDTH, topPos, 0, 0, TEX_WIDTH, TEX_HEIGHT );
|
||||
|
||||
// Draw border/inventory
|
||||
RenderSystem.color4f( 1.0F, 1.0F, 1.0F, 1.0F );
|
||||
minecraft.getTextureManager().bind( texture );
|
||||
blit( transform, leftPos, topPos, 0, 0, imageWidth, imageHeight );
|
||||
minecraft.getTextureManager().bind( advanced ? ComputerBorderRenderer.BACKGROUND_ADVANCED : ComputerBorderRenderer.BACKGROUND_NORMAL );
|
||||
ComputerSidebar.renderBackground( transform, leftPos, topPos + sidebarYOffset );
|
||||
|
||||
// Draw selection slot
|
||||
int slot = container.getSelectedSlot();
|
||||
int slot = getMenu().getSelectedSlot();
|
||||
if( slot >= 0 )
|
||||
{
|
||||
RenderSystem.color4f( 1.0F, 1.0F, 1.0F, 1.0F );
|
||||
int slotX = slot % 4;
|
||||
int slotY = slot / 4;
|
||||
blit( transform,
|
||||
leftPos + ContainerTurtle.TURTLE_START_X - 2 + slotX * 18,
|
||||
topPos + ContainerTurtle.PLAYER_START_Y - 2 + slotY * 18,
|
||||
leftPos + TURTLE_START_X - 2 + slotX * 18, topPos + PLAYER_START_Y - 2 + slotY * 18,
|
||||
0, 217, 24, 24
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render( @Nonnull MatrixStack stack, int mouseX, int mouseY, float partialTicks )
|
||||
{
|
||||
renderBackground( stack );
|
||||
super.render( stack, mouseX, mouseY, partialTicks );
|
||||
renderTooltip( stack, 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
|
||||
protected void renderLabels( @Nonnull MatrixStack transform, int mouseX, int mouseY )
|
||||
{
|
||||
// Skip rendering labels.
|
||||
}
|
||||
}
|
||||
|
127
src/main/java/dan200/computercraft/client/gui/OptionScreen.java
Normal file
127
src/main/java/dan200/computercraft/client/gui/OptionScreen.java
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.client.gui;
|
||||
|
||||
import com.mojang.blaze3d.matrix.MatrixStack;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.IBidiRenderer;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.widget.Widget;
|
||||
import net.minecraft.client.gui.widget.button.Button;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
|
||||
public final class OptionScreen extends Screen
|
||||
{
|
||||
private static final ResourceLocation BACKGROUND = new ResourceLocation( "computercraft", "textures/gui/blank_screen.png" );
|
||||
|
||||
public static final int BUTTON_WIDTH = 100;
|
||||
public static final int BUTTON_HEIGHT = 20;
|
||||
|
||||
private static final int PADDING = 16;
|
||||
private static final int FONT_HEIGHT = 9;
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
private int innerWidth;
|
||||
private int innerHeight;
|
||||
|
||||
private IBidiRenderer messageRenderer;
|
||||
private final ITextComponent message;
|
||||
private final List<Widget> buttons;
|
||||
private final Runnable exit;
|
||||
|
||||
private final Screen originalScreen;
|
||||
|
||||
private OptionScreen( ITextComponent title, ITextComponent message, List<Widget> buttons, Runnable exit, Screen originalScreen )
|
||||
{
|
||||
super( title );
|
||||
this.message = message;
|
||||
this.buttons = buttons;
|
||||
this.exit = exit;
|
||||
this.originalScreen = originalScreen;
|
||||
}
|
||||
|
||||
public static void show( Minecraft minecraft, ITextComponent title, ITextComponent message, List<Widget> buttons, Runnable exit )
|
||||
{
|
||||
minecraft.setScreen( new OptionScreen( title, message, buttons, exit, unwrap( minecraft.screen ) ) );
|
||||
}
|
||||
|
||||
public static Screen unwrap( Screen screen )
|
||||
{
|
||||
return screen instanceof OptionScreen ? ((OptionScreen) screen).getOriginalScreen() : screen;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init()
|
||||
{
|
||||
super.init();
|
||||
|
||||
int buttonWidth = BUTTON_WIDTH * buttons.size() + PADDING * (buttons.size() - 1);
|
||||
int innerWidth = this.innerWidth = Math.max( 256, buttonWidth + PADDING * 2 );
|
||||
|
||||
messageRenderer = IBidiRenderer.create( font, message, innerWidth - PADDING * 2 );
|
||||
|
||||
int textHeight = messageRenderer.getLineCount() * FONT_HEIGHT + PADDING * 2;
|
||||
innerHeight = textHeight + (buttons.isEmpty() ? 0 : buttons.get( 0 ).getHeight()) + PADDING;
|
||||
|
||||
x = (width - innerWidth) / 2;
|
||||
y = (height - innerHeight) / 2;
|
||||
|
||||
int x = (width - buttonWidth) / 2;
|
||||
for( Widget button : buttons )
|
||||
{
|
||||
button.x = x;
|
||||
button.y = y + textHeight;
|
||||
addButton( button );
|
||||
|
||||
x += BUTTON_WIDTH + PADDING;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render( @Nonnull MatrixStack transform, int mouseX, int mouseY, float partialTicks )
|
||||
{
|
||||
renderBackground( transform );
|
||||
|
||||
// Render the actual texture.
|
||||
minecraft.textureManager.bind( BACKGROUND );
|
||||
blit( transform, x, y, 0, 0, innerWidth, PADDING );
|
||||
blit( transform,
|
||||
x, y + PADDING, 0, PADDING, innerWidth, innerHeight - PADDING * 2,
|
||||
innerWidth, PADDING
|
||||
);
|
||||
blit( transform, x, y + innerHeight - PADDING, 0, 256 - PADDING, innerWidth, PADDING );
|
||||
|
||||
messageRenderer.renderLeftAlignedNoShadow( transform, x + PADDING, y + PADDING, FONT_HEIGHT, 0x404040 );
|
||||
super.render( transform, mouseX, mouseY, partialTicks );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose()
|
||||
{
|
||||
exit.run();
|
||||
}
|
||||
|
||||
public static Widget newButton( ITextComponent component, Button.IPressable clicked )
|
||||
{
|
||||
return new Button( 0, 0, BUTTON_WIDTH, BUTTON_HEIGHT, component, clicked );
|
||||
}
|
||||
|
||||
public void disable()
|
||||
{
|
||||
for( Widget widget : buttons ) widget.active = false;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Screen getOriginalScreen()
|
||||
{
|
||||
return originalScreen;
|
||||
}
|
||||
}
|
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.client.gui.widgets;
|
||||
|
||||
import com.mojang.blaze3d.matrix.MatrixStack;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.client.render.ComputerBorderRenderer;
|
||||
import dan200.computercraft.shared.computer.core.ClientComputer;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.widget.Widget;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.text.TextFormatting;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Registers buttons to interact with a computer.
|
||||
*/
|
||||
public final class ComputerSidebar
|
||||
{
|
||||
private static final ResourceLocation TEXTURE = new ResourceLocation( ComputerCraft.MOD_ID, "textures/gui/buttons.png" );
|
||||
|
||||
private static final int TEX_SIZE = 64;
|
||||
|
||||
private static final int ICON_WIDTH = 12;
|
||||
private static final int ICON_HEIGHT = 12;
|
||||
private static final int ICON_MARGIN = 2;
|
||||
|
||||
private static final int ICON_TEX_Y_DIFF = 14;
|
||||
|
||||
private static final int CORNERS_BORDER = 3;
|
||||
private static final int FULL_BORDER = CORNERS_BORDER + ICON_MARGIN;
|
||||
|
||||
private static final int BUTTONS = 2;
|
||||
private static final int HEIGHT = (ICON_HEIGHT + ICON_MARGIN * 2) * BUTTONS + CORNERS_BORDER * 2;
|
||||
public static final int WIDTH = 17;
|
||||
|
||||
private ComputerSidebar()
|
||||
{
|
||||
}
|
||||
|
||||
public static void addButtons( Screen screen, ClientComputer computer, Consumer<Widget> add, int x, int y )
|
||||
{
|
||||
x += CORNERS_BORDER + 1;
|
||||
y += CORNERS_BORDER + ICON_MARGIN;
|
||||
|
||||
add.accept( new DynamicImageButton(
|
||||
screen, x, y, ICON_WIDTH, ICON_HEIGHT, () -> computer.isOn() ? 15 : 1, 1, ICON_TEX_Y_DIFF,
|
||||
TEXTURE, TEX_SIZE, TEX_SIZE, b -> toggleComputer( computer ),
|
||||
() -> computer.isOn() ? Arrays.asList(
|
||||
new TranslationTextComponent( "gui.computercraft.tooltip.turn_off" ),
|
||||
new TranslationTextComponent( "gui.computercraft.tooltip.turn_off.key" ).withStyle( TextFormatting.GRAY )
|
||||
) : Arrays.asList(
|
||||
new TranslationTextComponent( "gui.computercraft.tooltip.turn_on" ),
|
||||
new TranslationTextComponent( "gui.computercraft.tooltip.turn_off.key" ).withStyle( TextFormatting.GRAY )
|
||||
)
|
||||
) );
|
||||
|
||||
y += ICON_HEIGHT + ICON_MARGIN * 2;
|
||||
|
||||
add.accept( new DynamicImageButton(
|
||||
screen, x, y, ICON_WIDTH, ICON_HEIGHT, 29, 1, ICON_TEX_Y_DIFF,
|
||||
TEXTURE, TEX_SIZE, TEX_SIZE, b -> computer.queueEvent( "terminate" ),
|
||||
Arrays.asList(
|
||||
new TranslationTextComponent( "gui.computercraft.tooltip.terminate" ),
|
||||
new TranslationTextComponent( "gui.computercraft.tooltip.terminate.key" ).withStyle( TextFormatting.GRAY )
|
||||
)
|
||||
) );
|
||||
}
|
||||
|
||||
public static void renderBackground( MatrixStack transform, int x, int y )
|
||||
{
|
||||
Screen.blit( transform,
|
||||
x, y, 0, 102, WIDTH, FULL_BORDER,
|
||||
ComputerBorderRenderer.TEX_SIZE, ComputerBorderRenderer.TEX_SIZE
|
||||
);
|
||||
|
||||
Screen.blit( transform,
|
||||
x, y + FULL_BORDER, WIDTH, HEIGHT - FULL_BORDER * 2,
|
||||
0, 107, WIDTH, 4,
|
||||
ComputerBorderRenderer.TEX_SIZE, ComputerBorderRenderer.TEX_SIZE
|
||||
);
|
||||
|
||||
Screen.blit( transform,
|
||||
x, y + HEIGHT - FULL_BORDER, 0, 111, WIDTH, FULL_BORDER,
|
||||
ComputerBorderRenderer.TEX_SIZE, ComputerBorderRenderer.TEX_SIZE
|
||||
);
|
||||
}
|
||||
|
||||
private static void toggleComputer( ClientComputer computer )
|
||||
{
|
||||
if( computer.isOn() )
|
||||
{
|
||||
computer.shutdown();
|
||||
}
|
||||
else
|
||||
{
|
||||
computer.turnOn();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.client.gui.widgets;
|
||||
|
||||
import com.mojang.blaze3d.matrix.MatrixStack;
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.widget.button.Button;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import net.minecraftforge.common.util.NonNullSupplier;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
import java.util.function.IntSupplier;
|
||||
|
||||
/**
|
||||
* Version of {@link net.minecraft.client.gui.widget.button.ImageButton} which allows changing some properties
|
||||
* dynamically.
|
||||
*/
|
||||
public class DynamicImageButton extends Button
|
||||
{
|
||||
private final Screen screen;
|
||||
private final ResourceLocation texture;
|
||||
private final IntSupplier xTexStart;
|
||||
private final int yTexStart;
|
||||
private final int yDiffTex;
|
||||
private final int textureWidth;
|
||||
private final int textureHeight;
|
||||
private final NonNullSupplier<List<ITextComponent>> tooltip;
|
||||
|
||||
public DynamicImageButton(
|
||||
Screen screen, int x, int y, int width, int height, int xTexStart, int yTexStart, int yDiffTex,
|
||||
ResourceLocation texture, int textureWidth, int textureHeight,
|
||||
IPressable onPress, List<ITextComponent> tooltip
|
||||
)
|
||||
{
|
||||
this(
|
||||
screen, x, y, width, height, () -> xTexStart, yTexStart, yDiffTex,
|
||||
texture, textureWidth, textureHeight,
|
||||
onPress, () -> tooltip
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public DynamicImageButton(
|
||||
Screen screen, int x, int y, int width, int height, IntSupplier xTexStart, int yTexStart, int yDiffTex,
|
||||
ResourceLocation texture, int textureWidth, int textureHeight,
|
||||
IPressable onPress, NonNullSupplier<List<ITextComponent>> tooltip
|
||||
)
|
||||
{
|
||||
super( x, y, width, height, StringTextComponent.EMPTY, onPress );
|
||||
this.screen = screen;
|
||||
this.textureWidth = textureWidth;
|
||||
this.textureHeight = textureHeight;
|
||||
this.xTexStart = xTexStart;
|
||||
this.yTexStart = yTexStart;
|
||||
this.yDiffTex = yDiffTex;
|
||||
this.texture = texture;
|
||||
this.tooltip = tooltip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderButton( @Nonnull MatrixStack stack, int mouseX, int mouseY, float partialTicks )
|
||||
{
|
||||
Minecraft minecraft = Minecraft.getInstance();
|
||||
minecraft.getTextureManager().bind( texture );
|
||||
RenderSystem.disableDepthTest();
|
||||
|
||||
int yTex = yTexStart;
|
||||
if( isHovered() ) yTex += yDiffTex;
|
||||
|
||||
blit( stack, x, y, xTexStart.getAsInt(), yTex, width, height, textureWidth, textureHeight );
|
||||
RenderSystem.enableDepthTest();
|
||||
|
||||
if( isHovered() ) renderToolTip( stack, mouseX, mouseY );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ITextComponent getMessage()
|
||||
{
|
||||
List<ITextComponent> tooltip = this.tooltip.get();
|
||||
return tooltip.isEmpty() ? StringTextComponent.EMPTY : tooltip.get( 0 );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderToolTip( @Nonnull MatrixStack stack, int mouseX, int mouseY )
|
||||
{
|
||||
List<ITextComponent> tooltip = this.tooltip.get();
|
||||
if( !tooltip.isEmpty() )
|
||||
{
|
||||
screen.renderWrappedToolTip( stack, tooltip, mouseX, mouseY, screen.getMinecraft().font );
|
||||
}
|
||||
}
|
||||
}
|
@@ -5,32 +5,35 @@
|
||||
*/
|
||||
package dan200.computercraft.client.gui.widgets;
|
||||
|
||||
import com.mojang.blaze3d.matrix.MatrixStack;
|
||||
import dan200.computercraft.client.gui.FixedWidthFontRenderer;
|
||||
import dan200.computercraft.core.terminal.Terminal;
|
||||
import dan200.computercraft.shared.computer.core.ClientComputer;
|
||||
import dan200.computercraft.shared.computer.core.IComputer;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.IGuiEventListener;
|
||||
import net.minecraft.client.gui.widget.Widget;
|
||||
import net.minecraft.util.SharedConstants;
|
||||
import net.minecraft.util.math.vector.Matrix4f;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.BitSet;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT;
|
||||
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_WIDTH;
|
||||
import static dan200.computercraft.client.render.ComputerBorderRenderer.MARGIN;
|
||||
|
||||
public class WidgetTerminal implements IGuiEventListener
|
||||
public class WidgetTerminal extends Widget
|
||||
{
|
||||
private static final float TERMINATE_TIME = 0.5f;
|
||||
|
||||
private final Minecraft client;
|
||||
private final ClientComputer computer;
|
||||
|
||||
private boolean focused;
|
||||
|
||||
private final Supplier<ClientComputer> computer;
|
||||
private final int termWidth;
|
||||
private final int termHeight;
|
||||
// The positions of the actual terminal
|
||||
private final int innerX;
|
||||
private final int innerY;
|
||||
private final int innerWidth;
|
||||
private final int innerHeight;
|
||||
|
||||
private float terminateTimer = -1;
|
||||
private float rebootTimer = -1;
|
||||
@@ -40,23 +43,18 @@ public class WidgetTerminal implements IGuiEventListener
|
||||
private int lastMouseX = -1;
|
||||
private int lastMouseY = -1;
|
||||
|
||||
private final int leftMargin;
|
||||
private final int rightMargin;
|
||||
private final int topMargin;
|
||||
private final int bottomMargin;
|
||||
|
||||
private final BitSet keysDown = new BitSet( 256 );
|
||||
|
||||
public WidgetTerminal( Minecraft client, Supplier<ClientComputer> computer, int termWidth, int termHeight, int leftMargin, int rightMargin, int topMargin, int bottomMargin )
|
||||
public WidgetTerminal( @Nonnull ClientComputer computer, int x, int y, int termWidth, int termHeight )
|
||||
{
|
||||
this.client = client;
|
||||
super( x, y, termWidth * FONT_WIDTH + MARGIN * 2, termHeight * FONT_HEIGHT + MARGIN * 2, StringTextComponent.EMPTY );
|
||||
|
||||
this.computer = computer;
|
||||
this.termWidth = termWidth;
|
||||
this.termHeight = termHeight;
|
||||
this.leftMargin = leftMargin;
|
||||
this.rightMargin = rightMargin;
|
||||
this.topMargin = topMargin;
|
||||
this.bottomMargin = bottomMargin;
|
||||
|
||||
innerX = x + MARGIN;
|
||||
innerY = y + MARGIN;
|
||||
innerWidth = termWidth * FONT_WIDTH;
|
||||
innerHeight = termHeight * FONT_HEIGHT;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -65,7 +63,7 @@ public class WidgetTerminal implements IGuiEventListener
|
||||
if( ch >= 32 && ch <= 126 || ch >= 160 && ch <= 255 ) // printable chars in byte range
|
||||
{
|
||||
// Queue the "char" event
|
||||
queueEvent( "char", Character.toString( ch ) );
|
||||
computer.queueEvent( "char", new Object[] { Character.toString( ch ) } );
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -91,7 +89,7 @@ public class WidgetTerminal implements IGuiEventListener
|
||||
|
||||
case GLFW.GLFW_KEY_V:
|
||||
// Ctrl+V for paste
|
||||
String clipboard = client.keyboardHandler.getClipboard();
|
||||
String clipboard = Minecraft.getInstance().keyboardHandler.getClipboard();
|
||||
if( clipboard != null )
|
||||
{
|
||||
// Clip to the first occurrence of \r or \n
|
||||
@@ -116,7 +114,7 @@ public class WidgetTerminal implements IGuiEventListener
|
||||
{
|
||||
// Clip to 512 characters and queue the event
|
||||
if( clipboard.length() > 512 ) clipboard = clipboard.substring( 0, 512 );
|
||||
queueEvent( "paste", clipboard );
|
||||
computer.queueEvent( "paste", new Object[] { clipboard } );
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -129,8 +127,7 @@ public class WidgetTerminal implements IGuiEventListener
|
||||
// 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 );
|
||||
computer.keyDown( key, repeat );
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -143,8 +140,7 @@ public class WidgetTerminal implements IGuiEventListener
|
||||
if( key >= 0 && keysDown.get( key ) )
|
||||
{
|
||||
keysDown.set( key, false );
|
||||
IComputer computer = this.computer.get();
|
||||
if( computer != null ) computer.keyUp( key );
|
||||
computer.keyUp( key );
|
||||
}
|
||||
|
||||
switch( key )
|
||||
@@ -170,14 +166,14 @@ public class WidgetTerminal implements IGuiEventListener
|
||||
@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;
|
||||
if( !inTermRegion( mouseX, mouseY ) ) return false;
|
||||
if( !computer.isColour() || button < 0 || button > 2 ) return false;
|
||||
|
||||
Terminal term = computer.getTerminal();
|
||||
if( term != null )
|
||||
{
|
||||
int charX = (int) (mouseX / FONT_WIDTH);
|
||||
int charY = (int) (mouseY / FONT_HEIGHT);
|
||||
int charX = (int) ((mouseX - innerX) / FONT_WIDTH);
|
||||
int charY = (int) ((mouseY - innerY) / FONT_HEIGHT);
|
||||
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
||||
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
||||
|
||||
@@ -194,14 +190,14 @@ public class WidgetTerminal implements IGuiEventListener
|
||||
@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;
|
||||
if( !inTermRegion( mouseX, mouseY ) ) return false;
|
||||
if( !computer.isColour() || button < 0 || button > 2 ) return false;
|
||||
|
||||
Terminal term = computer.getTerminal();
|
||||
if( term != null )
|
||||
{
|
||||
int charX = (int) (mouseX / FONT_WIDTH);
|
||||
int charY = (int) (mouseY / FONT_HEIGHT);
|
||||
int charX = (int) ((mouseX - innerX) / FONT_WIDTH);
|
||||
int charY = (int) ((mouseY - innerY) / FONT_HEIGHT);
|
||||
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
||||
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
||||
|
||||
@@ -221,14 +217,14 @@ public class WidgetTerminal implements IGuiEventListener
|
||||
@Override
|
||||
public boolean mouseDragged( double mouseX, double mouseY, int button, double v2, double v3 )
|
||||
{
|
||||
ClientComputer computer = this.computer.get();
|
||||
if( computer == null || !computer.isColour() || button < 0 || button > 2 ) return false;
|
||||
if( !inTermRegion( mouseX, mouseY ) ) return false;
|
||||
if( !computer.isColour() || button < 0 || button > 2 ) return false;
|
||||
|
||||
Terminal term = computer.getTerminal();
|
||||
if( term != null )
|
||||
{
|
||||
int charX = (int) (mouseX / FONT_WIDTH);
|
||||
int charY = (int) (mouseY / FONT_HEIGHT);
|
||||
int charX = (int) ((mouseX - innerX) / FONT_WIDTH);
|
||||
int charY = (int) ((mouseY - innerY) / FONT_HEIGHT);
|
||||
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
||||
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
||||
|
||||
@@ -246,14 +242,14 @@ public class WidgetTerminal implements IGuiEventListener
|
||||
@Override
|
||||
public boolean mouseScrolled( double mouseX, double mouseY, double delta )
|
||||
{
|
||||
ClientComputer computer = this.computer.get();
|
||||
if( computer == null || !computer.isColour() || delta == 0 ) return false;
|
||||
if( !inTermRegion( mouseX, mouseY ) ) return false;
|
||||
if( !computer.isColour() || delta == 0 ) return false;
|
||||
|
||||
Terminal term = computer.getTerminal();
|
||||
if( term != null )
|
||||
{
|
||||
int charX = (int) (mouseX / FONT_WIDTH);
|
||||
int charY = (int) (mouseY / FONT_HEIGHT);
|
||||
int charX = (int) ((mouseX - innerX) / FONT_WIDTH);
|
||||
int charY = (int) ((mouseY - innerY) / FONT_HEIGHT);
|
||||
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
||||
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
||||
|
||||
@@ -266,89 +262,74 @@ public class WidgetTerminal implements IGuiEventListener
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean inTermRegion( double mouseX, double mouseY )
|
||||
{
|
||||
return active && visible && mouseX >= innerX && mouseY >= innerY && mouseX < innerX + innerWidth && mouseY < innerY + innerHeight;
|
||||
}
|
||||
|
||||
public void update()
|
||||
{
|
||||
if( terminateTimer >= 0 && terminateTimer < TERMINATE_TIME && (terminateTimer += 0.05f) > TERMINATE_TIME )
|
||||
{
|
||||
queueEvent( "terminate" );
|
||||
computer.queueEvent( "terminate" );
|
||||
}
|
||||
|
||||
if( shutdownTimer >= 0 && shutdownTimer < TERMINATE_TIME && (shutdownTimer += 0.05f) > TERMINATE_TIME )
|
||||
{
|
||||
ClientComputer computer = this.computer.get();
|
||||
if( computer != null ) computer.shutdown();
|
||||
computer.shutdown();
|
||||
}
|
||||
|
||||
if( rebootTimer >= 0 && rebootTimer < TERMINATE_TIME && (rebootTimer += 0.05f) > TERMINATE_TIME )
|
||||
{
|
||||
ClientComputer computer = this.computer.get();
|
||||
if( computer != null ) computer.reboot();
|
||||
computer.reboot();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean changeFocus( boolean reversed )
|
||||
public void onFocusedChanged( boolean focused )
|
||||
{
|
||||
if( focused )
|
||||
if( !focused )
|
||||
{
|
||||
// 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 );
|
||||
if( keysDown.get( key ) ) computer.keyUp( key );
|
||||
}
|
||||
keysDown.clear();
|
||||
|
||||
// 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 );
|
||||
computer.mouseUp( lastMouseButton + 1, lastMouseX + 1, lastMouseY + 1 );
|
||||
lastMouseButton = -1;
|
||||
}
|
||||
|
||||
shutdownTimer = terminateTimer = rebootTimer = -1;
|
||||
}
|
||||
focused = !focused;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void draw( int originX, int originY )
|
||||
{
|
||||
synchronized( computer )
|
||||
{
|
||||
// Draw the screen contents
|
||||
ClientComputer computer = this.computer.get();
|
||||
Terminal terminal = computer != null ? computer.getTerminal() : null;
|
||||
if( terminal != null )
|
||||
{
|
||||
FixedWidthFontRenderer.drawTerminal( originX, originY, terminal, !computer.isColour(), topMargin, bottomMargin, leftMargin, rightMargin );
|
||||
}
|
||||
else
|
||||
{
|
||||
FixedWidthFontRenderer.drawEmptyTerminal(
|
||||
originX - leftMargin, originY - rightMargin,
|
||||
termWidth * FONT_WIDTH + leftMargin + rightMargin,
|
||||
termHeight * FONT_HEIGHT + topMargin + bottomMargin
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void queueEvent( String event )
|
||||
{
|
||||
ClientComputer computer = this.computer.get();
|
||||
if( computer != null ) computer.queueEvent( event );
|
||||
}
|
||||
|
||||
private void queueEvent( String event, Object... args )
|
||||
{
|
||||
ClientComputer computer = this.computer.get();
|
||||
if( computer != null ) computer.queueEvent( event, args );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMouseOver( double x, double y )
|
||||
public void render( @Nonnull MatrixStack transform, int mouseX, int mouseY, float partialTicks )
|
||||
{
|
||||
return true;
|
||||
Matrix4f matrix = transform.last().pose();
|
||||
Terminal terminal = computer.getTerminal();
|
||||
if( terminal != null )
|
||||
{
|
||||
FixedWidthFontRenderer.drawTerminal( matrix, innerX, innerY, terminal, !computer.isColour(), MARGIN, MARGIN, MARGIN, MARGIN );
|
||||
}
|
||||
else
|
||||
{
|
||||
FixedWidthFontRenderer.drawEmptyTerminal( matrix, x, y, width, height );
|
||||
}
|
||||
}
|
||||
|
||||
public static int getWidth( int termWidth )
|
||||
{
|
||||
return termWidth * FONT_WIDTH + MARGIN * 2;
|
||||
}
|
||||
|
||||
public static int getHeight( int termHeight )
|
||||
{
|
||||
return termHeight * FONT_HEIGHT + MARGIN * 2;
|
||||
}
|
||||
}
|
||||
|
@@ -1,105 +0,0 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. 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 boolean changeFocus( boolean b )
|
||||
{
|
||||
return listener.changeFocus( b );
|
||||
}
|
||||
|
||||
@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 x, double y, double delta )
|
||||
{
|
||||
double dx = x - this.x, dy = y - this.y;
|
||||
return dx >= 0 && dx < width && dy >= 0 && dy < height && listener.mouseScrolled( dx, dy, 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMouseOver( double x, double y )
|
||||
{
|
||||
double dx = x - this.x, dy = y - this.y;
|
||||
return dx >= 0 && dx < width && dy >= 0 && dy < height;
|
||||
}
|
||||
}
|
@@ -1,108 +0,0 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.client.proxy;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.client.gui.*;
|
||||
import dan200.computercraft.client.render.TileEntityMonitorRenderer;
|
||||
import dan200.computercraft.client.render.TileEntityTurtleRenderer;
|
||||
import dan200.computercraft.client.render.TurtlePlayerRenderer;
|
||||
import dan200.computercraft.shared.Registry;
|
||||
import dan200.computercraft.shared.common.IColouredItem;
|
||||
import dan200.computercraft.shared.computer.inventory.ContainerComputer;
|
||||
import dan200.computercraft.shared.computer.inventory.ContainerViewComputer;
|
||||
import dan200.computercraft.shared.peripheral.monitor.ClientMonitor;
|
||||
import dan200.computercraft.shared.pocket.inventory.ContainerPocketComputer;
|
||||
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
|
||||
import net.minecraft.client.gui.ScreenManager;
|
||||
import net.minecraft.client.renderer.RenderType;
|
||||
import net.minecraft.client.renderer.RenderTypeLookup;
|
||||
import net.minecraft.item.IItemPropertyGetter;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemModelsProperties;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.client.registry.ClientRegistry;
|
||||
import net.minecraftforge.fml.client.registry.RenderingRegistry;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD )
|
||||
public final class ComputerCraftProxyClient
|
||||
{
|
||||
@SubscribeEvent
|
||||
public static void setupClient( FMLClientSetupEvent event )
|
||||
{
|
||||
registerContainers();
|
||||
|
||||
// While turtles themselves are not transparent, their upgrades may be.
|
||||
RenderTypeLookup.setRenderLayer( Registry.ModBlocks.TURTLE_NORMAL.get(), RenderType.translucent() );
|
||||
RenderTypeLookup.setRenderLayer( Registry.ModBlocks.TURTLE_ADVANCED.get(), RenderType.translucent() );
|
||||
|
||||
// Monitors' textures have transparent fronts and so count as cutouts.
|
||||
RenderTypeLookup.setRenderLayer( Registry.ModBlocks.MONITOR_NORMAL.get(), RenderType.cutout() );
|
||||
RenderTypeLookup.setRenderLayer( Registry.ModBlocks.MONITOR_ADVANCED.get(), RenderType.cutout() );
|
||||
|
||||
// Setup TESRs
|
||||
ClientRegistry.bindTileEntityRenderer( Registry.ModTiles.MONITOR_NORMAL.get(), TileEntityMonitorRenderer::new );
|
||||
ClientRegistry.bindTileEntityRenderer( Registry.ModTiles.MONITOR_ADVANCED.get(), TileEntityMonitorRenderer::new );
|
||||
ClientRegistry.bindTileEntityRenderer( Registry.ModTiles.TURTLE_NORMAL.get(), TileEntityTurtleRenderer::new );
|
||||
ClientRegistry.bindTileEntityRenderer( Registry.ModTiles.TURTLE_ADVANCED.get(), TileEntityTurtleRenderer::new );
|
||||
|
||||
RenderingRegistry.registerEntityRenderingHandler( Registry.ModEntities.TURTLE_PLAYER.get(), TurtlePlayerRenderer::new );
|
||||
|
||||
registerItemProperty( "state",
|
||||
( stack, world, player ) -> ItemPocketComputer.getState( stack ).ordinal(),
|
||||
Registry.ModItems.POCKET_COMPUTER_NORMAL, Registry.ModItems.POCKET_COMPUTER_ADVANCED
|
||||
);
|
||||
registerItemProperty( "state",
|
||||
( stack, world, player ) -> IColouredItem.getColourBasic( stack ) != -1 ? 1 : 0,
|
||||
Registry.ModItems.POCKET_COMPUTER_NORMAL, Registry.ModItems.POCKET_COMPUTER_ADVANCED
|
||||
);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
private static void registerItemProperty( String name, IItemPropertyGetter getter, Supplier<? extends Item>... items )
|
||||
{
|
||||
ResourceLocation id = new ResourceLocation( ComputerCraft.MOD_ID, name );
|
||||
for( Supplier<? extends Item> item : items )
|
||||
{
|
||||
ItemModelsProperties.register( item.get(), id, getter );
|
||||
}
|
||||
}
|
||||
|
||||
private static void registerContainers()
|
||||
{
|
||||
// My IDE doesn't think so, but we do actually need these generics.
|
||||
|
||||
ScreenManager.<ContainerComputer, GuiComputer<ContainerComputer>>register( Registry.ModContainers.COMPUTER.get(), GuiComputer::create );
|
||||
ScreenManager.<ContainerPocketComputer, GuiComputer<ContainerPocketComputer>>register( Registry.ModContainers.POCKET_COMPUTER.get(), GuiComputer::createPocket );
|
||||
ScreenManager.register( Registry.ModContainers.TURTLE.get(), GuiTurtle::new );
|
||||
|
||||
ScreenManager.register( Registry.ModContainers.PRINTER.get(), GuiPrinter::new );
|
||||
ScreenManager.register( Registry.ModContainers.DISK_DRIVE.get(), GuiDiskDrive::new );
|
||||
ScreenManager.register( Registry.ModContainers.PRINTOUT.get(), GuiPrintout::new );
|
||||
|
||||
ScreenManager.<ContainerViewComputer, GuiComputer<ContainerViewComputer>>register( Registry.ModContainers.VIEW_COMPUTER.get(), GuiComputer::createView );
|
||||
}
|
||||
|
||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Dist.CLIENT )
|
||||
public static final class ForgeHandlers
|
||||
{
|
||||
@SubscribeEvent
|
||||
public static void onWorldUnload( WorldEvent.Unload event )
|
||||
{
|
||||
if( event.getWorld().isClientSide() )
|
||||
{
|
||||
ClientMonitor.destroyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -52,7 +52,8 @@ public class ComputerBorderRenderer
|
||||
|
||||
public static final int LIGHT_HEIGHT = 8;
|
||||
|
||||
private static final float TEX_SCALE = 1 / 256.0f;
|
||||
public static final int TEX_SIZE = 256;
|
||||
private static final float TEX_SCALE = 1 / (float) TEX_SIZE;
|
||||
|
||||
private final Matrix4f transform;
|
||||
private final IVertexBuilder builder;
|
||||
|
@@ -37,7 +37,7 @@ public class HTTPAPI implements ILuaAPI
|
||||
{
|
||||
private final IAPIEnvironment apiEnvironment;
|
||||
|
||||
private final ResourceGroup<CheckUrl> checkUrls = new ResourceGroup<>();
|
||||
private final ResourceGroup<CheckUrl> checkUrls = new ResourceGroup<>( ResourceGroup.DEFAULT );
|
||||
private final ResourceGroup<HttpRequest> requests = new ResourceQueue<>( () -> ComputerCraft.httpMaxRequests );
|
||||
private final ResourceGroup<Websocket> websockets = new ResourceGroup<>( () -> ComputerCraft.httpMaxWebsockets );
|
||||
|
||||
@@ -127,7 +127,10 @@ public class HTTPAPI implements ILuaAPI
|
||||
HttpRequest request = new HttpRequest( requests, apiEnvironment, address, postString, headers, binary, redirect );
|
||||
|
||||
// Make the request
|
||||
request.queue( r -> r.request( uri, httpMethod ) );
|
||||
if( !request.queue( r -> r.request( uri, httpMethod ) ) )
|
||||
{
|
||||
throw new LuaException( "Too many ongoing HTTP requests" );
|
||||
}
|
||||
|
||||
return new Object[] { true };
|
||||
}
|
||||
@@ -138,12 +141,15 @@ public class HTTPAPI implements ILuaAPI
|
||||
}
|
||||
|
||||
@LuaFunction
|
||||
public final Object[] checkURL( String address )
|
||||
public final Object[] checkURL( String address ) throws LuaException
|
||||
{
|
||||
try
|
||||
{
|
||||
URI uri = HttpRequest.checkUri( address );
|
||||
new CheckUrl( checkUrls, apiEnvironment, address, uri ).queue( CheckUrl::run );
|
||||
if( !new CheckUrl( checkUrls, apiEnvironment, address, uri ).queue( CheckUrl::run ) )
|
||||
{
|
||||
throw new LuaException( "Too many ongoing checkUrl calls" );
|
||||
}
|
||||
|
||||
return new Object[] { true };
|
||||
}
|
||||
|
@@ -11,12 +11,19 @@ import dan200.computercraft.core.apis.http.options.AddressRule;
|
||||
import dan200.computercraft.core.apis.http.options.Options;
|
||||
import dan200.computercraft.shared.util.ThreadUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ConnectTimeoutException;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.handler.codec.DecoderException;
|
||||
import io.netty.handler.codec.TooLongFrameException;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketHandshakeException;
|
||||
import io.netty.handler.ssl.SslContext;
|
||||
import io.netty.handler.ssl.SslContextBuilder;
|
||||
import io.netty.handler.timeout.ReadTimeoutException;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.net.ssl.SSLException;
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
@@ -161,4 +168,29 @@ public final class NetworkUtils
|
||||
buffer.readBytes( bytes );
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static String toFriendlyError( @Nonnull Throwable cause )
|
||||
{
|
||||
if( cause instanceof WebSocketHandshakeException || cause instanceof HTTPRequestException )
|
||||
{
|
||||
return cause.getMessage();
|
||||
}
|
||||
else if( cause instanceof TooLongFrameException )
|
||||
{
|
||||
return "Message is too large";
|
||||
}
|
||||
else if( cause instanceof ReadTimeoutException || cause instanceof ConnectTimeoutException )
|
||||
{
|
||||
return "Timed out";
|
||||
}
|
||||
else if( cause instanceof SSLHandshakeException || (cause instanceof DecoderException && cause.getCause() instanceof SSLHandshakeException) )
|
||||
{
|
||||
return "Could not create a secure connection";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "Could not connect";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -97,7 +97,7 @@ public abstract class Resource<T extends Resource<T>> implements Closeable
|
||||
tryClose();
|
||||
}
|
||||
|
||||
public boolean queue( Consumer<T> task )
|
||||
public final boolean queue( Consumer<T> task )
|
||||
{
|
||||
@SuppressWarnings( "unchecked" )
|
||||
T thisT = (T) this;
|
||||
|
@@ -18,6 +18,9 @@ import java.util.function.Supplier;
|
||||
*/
|
||||
public class ResourceGroup<T extends Resource<T>>
|
||||
{
|
||||
public static final int DEFAULT_LIMIT = 512;
|
||||
public static final IntSupplier DEFAULT = () -> DEFAULT_LIMIT;
|
||||
|
||||
private static final IntSupplier ZERO = () -> 0;
|
||||
|
||||
final IntSupplier limit;
|
||||
|
@@ -38,8 +38,10 @@ public class ResourceQueue<T extends Resource<T>> extends ResourceGroup<T>
|
||||
public synchronized boolean queue( Supplier<T> resource )
|
||||
{
|
||||
if( !active ) return false;
|
||||
if( super.queue( resource ) ) return true;
|
||||
if( pending.size() > DEFAULT_LIMIT ) return false;
|
||||
|
||||
if( !super.queue( resource ) ) pending.add( resource );
|
||||
pending.add( resource );
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@@ -19,13 +19,10 @@ import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.ConnectTimeoutException;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.codec.TooLongFrameException;
|
||||
import io.netty.handler.codec.http.*;
|
||||
import io.netty.handler.ssl.SslContext;
|
||||
import io.netty.handler.timeout.ReadTimeoutException;
|
||||
import io.netty.handler.timeout.ReadTimeoutHandler;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
@@ -190,7 +187,7 @@ public class HttpRequest extends Resource<HttpRequest>
|
||||
.remoteAddress( socketAddress )
|
||||
.connect()
|
||||
.addListener( c -> {
|
||||
if( !c.isSuccess() ) failure( c.cause() );
|
||||
if( !c.isSuccess() ) failure( NetworkUtils.toFriendlyError( c.cause() ) );
|
||||
} );
|
||||
|
||||
// Do an additional check for cancellation
|
||||
@@ -202,7 +199,7 @@ public class HttpRequest extends Resource<HttpRequest>
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
failure( "Could not connect" );
|
||||
failure( NetworkUtils.toFriendlyError( e ) );
|
||||
if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error in HTTP request", e );
|
||||
}
|
||||
}
|
||||
@@ -212,29 +209,6 @@ public class HttpRequest extends Resource<HttpRequest>
|
||||
if( tryClose() ) environment.queueEvent( FAILURE_EVENT, address, message );
|
||||
}
|
||||
|
||||
void failure( Throwable cause )
|
||||
{
|
||||
String message;
|
||||
if( cause instanceof HTTPRequestException )
|
||||
{
|
||||
message = cause.getMessage();
|
||||
}
|
||||
else if( cause instanceof TooLongFrameException )
|
||||
{
|
||||
message = "Response is too large";
|
||||
}
|
||||
else if( cause instanceof ReadTimeoutException || cause instanceof ConnectTimeoutException )
|
||||
{
|
||||
message = "Timed out";
|
||||
}
|
||||
else
|
||||
{
|
||||
message = "Could not connect";
|
||||
}
|
||||
|
||||
failure( message );
|
||||
}
|
||||
|
||||
void failure( String message, HttpResponseHandle object )
|
||||
{
|
||||
if( tryClose() ) environment.queueEvent( FAILURE_EVENT, address, message, object );
|
||||
|
@@ -183,7 +183,7 @@ public final class HttpRequestHandler extends SimpleChannelInboundHandler<HttpOb
|
||||
public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause )
|
||||
{
|
||||
if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error handling HTTP response", cause );
|
||||
request.failure( cause );
|
||||
request.failure( NetworkUtils.toFriendlyError( cause ) );
|
||||
}
|
||||
|
||||
private void sendResponse()
|
||||
|
@@ -22,6 +22,7 @@ import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.codec.http.HttpClientCodec;
|
||||
import io.netty.handler.codec.http.HttpHeaderNames;
|
||||
import io.netty.handler.codec.http.HttpHeaders;
|
||||
import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
|
||||
@@ -150,8 +151,9 @@ public class Websocket extends Resource<Websocket>
|
||||
p.addLast( sslContext.newHandler( ch.alloc(), uri.getHost(), socketAddress.getPort() ) );
|
||||
}
|
||||
|
||||
String subprotocol = headers.get( HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL );
|
||||
WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(
|
||||
uri, WebSocketVersion.V13, null, true, headers,
|
||||
uri, WebSocketVersion.V13, subprotocol, true, headers,
|
||||
options.websocketMessage <= 0 ? MAX_MESSAGE_SIZE : options.websocketMessage
|
||||
);
|
||||
|
||||
@@ -166,7 +168,7 @@ public class Websocket extends Resource<Websocket>
|
||||
.remoteAddress( socketAddress )
|
||||
.connect()
|
||||
.addListener( c -> {
|
||||
if( !c.isSuccess() ) failure( c.cause().getMessage() );
|
||||
if( !c.isSuccess() ) failure( NetworkUtils.toFriendlyError( c.cause() ) );
|
||||
} );
|
||||
|
||||
// Do an additional check for cancellation
|
||||
@@ -178,7 +180,7 @@ public class Websocket extends Resource<Websocket>
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
failure( "Could not connect" );
|
||||
failure( NetworkUtils.toFriendlyError( e ) );
|
||||
if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error in websocket", e );
|
||||
}
|
||||
}
|
||||
|
@@ -5,17 +5,13 @@
|
||||
*/
|
||||
package dan200.computercraft.core.apis.http.websocket;
|
||||
|
||||
import dan200.computercraft.core.apis.http.HTTPRequestException;
|
||||
import dan200.computercraft.core.apis.http.NetworkUtils;
|
||||
import dan200.computercraft.core.apis.http.options.Options;
|
||||
import dan200.computercraft.core.tracking.TrackingField;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ConnectTimeoutException;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.handler.codec.TooLongFrameException;
|
||||
import io.netty.handler.codec.http.FullHttpResponse;
|
||||
import io.netty.handler.codec.http.websocketx.*;
|
||||
import io.netty.handler.timeout.ReadTimeoutException;
|
||||
import io.netty.util.CharsetUtil;
|
||||
|
||||
import static dan200.computercraft.core.apis.http.websocket.Websocket.MESSAGE_EVENT;
|
||||
@@ -97,24 +93,7 @@ public class WebsocketHandler extends SimpleChannelInboundHandler<Object>
|
||||
{
|
||||
ctx.close();
|
||||
|
||||
String message;
|
||||
if( cause instanceof WebSocketHandshakeException || cause instanceof HTTPRequestException )
|
||||
{
|
||||
message = cause.getMessage();
|
||||
}
|
||||
else if( cause instanceof TooLongFrameException )
|
||||
{
|
||||
message = "Message is too large";
|
||||
}
|
||||
else if( cause instanceof ReadTimeoutException || cause instanceof ConnectTimeoutException )
|
||||
{
|
||||
message = "Timed out";
|
||||
}
|
||||
else
|
||||
{
|
||||
message = "Could not connect";
|
||||
}
|
||||
|
||||
String message = NetworkUtils.toFriendlyError( cause );
|
||||
if( handshaker.isHandshakeComplete() )
|
||||
{
|
||||
websocket.close( -1, message );
|
||||
|
@@ -11,7 +11,10 @@ import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.primitives.Primitives;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.lua.*;
|
||||
import dan200.computercraft.api.lua.IArguments;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
import dan200.computercraft.api.lua.LuaFunction;
|
||||
import dan200.computercraft.api.lua.MethodResult;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Type;
|
||||
@@ -63,7 +66,7 @@ public final class Generator<T>
|
||||
{
|
||||
this.base = base;
|
||||
this.context = context;
|
||||
this.interfaces = new String[] { Type.getInternalName( base ) };
|
||||
interfaces = new String[] { Type.getInternalName( base ) };
|
||||
this.wrap = wrap;
|
||||
|
||||
StringBuilder methodDesc = new StringBuilder().append( "(Ljava/lang/Object;" );
|
||||
|
@@ -251,6 +251,20 @@ final class ComputerExecutor
|
||||
* and then schedule a shutdown.
|
||||
*/
|
||||
void abort()
|
||||
{
|
||||
immediateFail( StateCommand.ABORT );
|
||||
}
|
||||
|
||||
/**
|
||||
* Abort this whole computer due to an internal error. This will immediately destroy the Lua machine,
|
||||
* and then schedule a shutdown.
|
||||
*/
|
||||
void fastFail()
|
||||
{
|
||||
immediateFail( StateCommand.ERROR );
|
||||
}
|
||||
|
||||
private void immediateFail( StateCommand command )
|
||||
{
|
||||
ILuaMachine machine = this.machine;
|
||||
if( machine != null ) machine.close();
|
||||
@@ -258,7 +272,7 @@ final class ComputerExecutor
|
||||
synchronized( queueLock )
|
||||
{
|
||||
if( closed ) return;
|
||||
command = StateCommand.ABORT;
|
||||
this.command = command;
|
||||
if( isOn ) enqueue();
|
||||
}
|
||||
}
|
||||
@@ -596,6 +610,12 @@ final class ComputerExecutor
|
||||
displayFailure( "Error running computer", TimeoutState.ABORT_MESSAGE );
|
||||
shutdown();
|
||||
break;
|
||||
|
||||
case ERROR:
|
||||
if( !isOn ) return;
|
||||
displayFailure( "Error running computer", "An internal error occurred, see logs." );
|
||||
shutdown();
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if( event != null )
|
||||
@@ -644,6 +664,7 @@ final class ComputerExecutor
|
||||
SHUTDOWN,
|
||||
REBOOT,
|
||||
ABORT,
|
||||
ERROR,
|
||||
}
|
||||
|
||||
private static final class Event
|
||||
|
@@ -506,6 +506,8 @@ public final class ComputerThread
|
||||
catch( Exception | LinkageError | VirtualMachineError e )
|
||||
{
|
||||
ComputerCraft.log.error( "Error running task on computer #" + executor.getComputer().getID(), e );
|
||||
// Tear down the computer immediately. There's no guarantee it's well behaved from now on.
|
||||
executor.fastFail();
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
@@ -10,7 +10,6 @@ import dan200.computercraft.api.lua.*;
|
||||
import dan200.computercraft.core.asm.LuaMethod;
|
||||
import dan200.computercraft.core.asm.ObjectSource;
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
import dan200.computercraft.core.computer.MainThread;
|
||||
import dan200.computercraft.core.computer.TimeoutState;
|
||||
import dan200.computercraft.core.tracking.Tracking;
|
||||
import dan200.computercraft.core.tracking.TrackingField;
|
||||
@@ -53,7 +52,7 @@ public class CobaltLuaMachine implements ILuaMachine
|
||||
private final Computer computer;
|
||||
private final TimeoutState timeout;
|
||||
private final TimeoutDebugHandler debug;
|
||||
private final ILuaContext context = new CobaltLuaContext();
|
||||
private final ILuaContext context;
|
||||
|
||||
private LuaState state;
|
||||
private LuaTable globals;
|
||||
@@ -65,6 +64,7 @@ public class CobaltLuaMachine implements ILuaMachine
|
||||
{
|
||||
this.computer = computer;
|
||||
this.timeout = timeout;
|
||||
context = new LuaContext( computer );
|
||||
debug = new TimeoutDebugHandler();
|
||||
|
||||
// Create an environment to run in
|
||||
@@ -97,7 +97,7 @@ public class CobaltLuaMachine implements ILuaMachine
|
||||
globals.load( state, new CoroutineLib() );
|
||||
globals.load( state, new Bit32Lib() );
|
||||
globals.load( state, new Utf8Lib() );
|
||||
if( ComputerCraft.debugEnable ) globals.load( state, new DebugLib() );
|
||||
globals.load( state, new DebugLib() );
|
||||
|
||||
// Remove globals we don't want to expose
|
||||
globals.rawset( "collectgarbage", Constants.NIL );
|
||||
@@ -509,53 +509,6 @@ public class CobaltLuaMachine implements ILuaMachine
|
||||
}
|
||||
}
|
||||
|
||||
private class CobaltLuaContext implements ILuaContext
|
||||
{
|
||||
@Override
|
||||
public long issueMainThreadTask( @Nonnull final ILuaTask task ) throws LuaException
|
||||
{
|
||||
// Issue command
|
||||
final long taskID = MainThread.getUniqueTaskID();
|
||||
final Runnable iTask = () -> {
|
||||
try
|
||||
{
|
||||
Object[] results = task.execute();
|
||||
if( results != null )
|
||||
{
|
||||
Object[] eventArguments = new Object[results.length + 2];
|
||||
eventArguments[0] = taskID;
|
||||
eventArguments[1] = true;
|
||||
System.arraycopy( results, 0, eventArguments, 2, results.length );
|
||||
computer.queueEvent( "task_complete", eventArguments );
|
||||
}
|
||||
else
|
||||
{
|
||||
computer.queueEvent( "task_complete", new Object[] { taskID, true } );
|
||||
}
|
||||
}
|
||||
catch( LuaException e )
|
||||
{
|
||||
computer.queueEvent( "task_complete", new Object[] { taskID, false, e.getMessage() } );
|
||||
}
|
||||
catch( Throwable t )
|
||||
{
|
||||
if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error running task", t );
|
||||
computer.queueEvent( "task_complete", new Object[] {
|
||||
taskID, false, "Java Exception Thrown: " + t,
|
||||
} );
|
||||
}
|
||||
};
|
||||
if( computer.queueMainThread( iTask ) )
|
||||
{
|
||||
return taskID;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new LuaException( "Task limit exceeded" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final class HardAbortError extends Error
|
||||
{
|
||||
private static final long serialVersionUID = 7954092008586367501L;
|
||||
|
69
src/main/java/dan200/computercraft/core/lua/LuaContext.java
Normal file
69
src/main/java/dan200/computercraft/core/lua/LuaContext.java
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.core.lua;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.lua.ILuaTask;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
import dan200.computercraft.core.computer.MainThread;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
class LuaContext implements ILuaContext
|
||||
{
|
||||
private final Computer computer;
|
||||
|
||||
LuaContext( Computer computer )
|
||||
{
|
||||
this.computer = computer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long issueMainThreadTask( @Nonnull final ILuaTask task ) throws LuaException
|
||||
{
|
||||
// Issue command
|
||||
final long taskID = MainThread.getUniqueTaskID();
|
||||
final Runnable iTask = () -> {
|
||||
try
|
||||
{
|
||||
Object[] results = task.execute();
|
||||
if( results != null )
|
||||
{
|
||||
Object[] eventArguments = new Object[results.length + 2];
|
||||
eventArguments[0] = taskID;
|
||||
eventArguments[1] = true;
|
||||
System.arraycopy( results, 0, eventArguments, 2, results.length );
|
||||
computer.queueEvent( "task_complete", eventArguments );
|
||||
}
|
||||
else
|
||||
{
|
||||
computer.queueEvent( "task_complete", new Object[] { taskID, true } );
|
||||
}
|
||||
}
|
||||
catch( LuaException e )
|
||||
{
|
||||
computer.queueEvent( "task_complete", new Object[] { taskID, false, e.getMessage() } );
|
||||
}
|
||||
catch( Exception t )
|
||||
{
|
||||
if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error running task", t );
|
||||
computer.queueEvent( "task_complete", new Object[] {
|
||||
taskID, false, "Java Exception Thrown: " + t,
|
||||
} );
|
||||
}
|
||||
};
|
||||
if( computer.queueMainThread( iTask ) )
|
||||
{
|
||||
return taskID;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new LuaException( "Task limit exceeded" );
|
||||
}
|
||||
}
|
||||
}
|
@@ -44,9 +44,9 @@ public class Terminal
|
||||
this.height = height;
|
||||
onChanged = changedCallback;
|
||||
|
||||
text = new TextBuffer[this.height];
|
||||
textColour = new TextBuffer[this.height];
|
||||
backgroundColour = new TextBuffer[this.height];
|
||||
text = new TextBuffer[height];
|
||||
textColour = new TextBuffer[height];
|
||||
backgroundColour = new TextBuffer[height];
|
||||
for( int i = 0; i < this.height; i++ )
|
||||
{
|
||||
text[i] = new TextBuffer( ' ', this.width );
|
||||
@@ -93,9 +93,9 @@ public class Terminal
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
text = new TextBuffer[this.height];
|
||||
textColour = new TextBuffer[this.height];
|
||||
backgroundColour = new TextBuffer[this.height];
|
||||
text = new TextBuffer[height];
|
||||
textColour = new TextBuffer[height];
|
||||
backgroundColour = new TextBuffer[height];
|
||||
for( int i = 0; i < this.height; i++ )
|
||||
{
|
||||
if( i >= oldHeight )
|
||||
|
@@ -12,7 +12,7 @@ public class TextBuffer
|
||||
public TextBuffer( char c, int length )
|
||||
{
|
||||
text = new char[length];
|
||||
this.fill( c );
|
||||
fill( c );
|
||||
}
|
||||
|
||||
public TextBuffer( String text )
|
||||
@@ -79,6 +79,7 @@ public class TextBuffer
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new String( text );
|
||||
|
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.core.tracking;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
import net.minecraft.util.text.LanguageMap;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.management.*;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
public final class ComputerMBean implements DynamicMBean, Tracker
|
||||
{
|
||||
private static final Set<TrackingField> SKIP = new HashSet<>( Arrays.asList(
|
||||
TrackingField.TASKS, TrackingField.TOTAL_TIME, TrackingField.AVERAGE_TIME, TrackingField.MAX_TIME,
|
||||
TrackingField.SERVER_COUNT, TrackingField.SERVER_TIME
|
||||
) );
|
||||
|
||||
private static ComputerMBean instance;
|
||||
|
||||
private final Map<String, LongSupplier> attributes = new HashMap<>();
|
||||
private final Map<TrackingField, Counter> values = new HashMap<>();
|
||||
private final MBeanInfo info;
|
||||
|
||||
private ComputerMBean()
|
||||
{
|
||||
List<MBeanAttributeInfo> attributes = new ArrayList<>();
|
||||
for( Map.Entry<String, TrackingField> field : TrackingField.fields().entrySet() )
|
||||
{
|
||||
if( SKIP.contains( field.getValue() ) ) continue;
|
||||
|
||||
String name = CaseFormat.LOWER_UNDERSCORE.to( CaseFormat.LOWER_CAMEL, field.getKey() );
|
||||
add( name, field.getValue(), attributes, null );
|
||||
}
|
||||
|
||||
add( "task", TrackingField.TOTAL_TIME, attributes, TrackingField.TASKS );
|
||||
add( "serverTask", TrackingField.SERVER_TIME, attributes, TrackingField.SERVER_COUNT );
|
||||
|
||||
this.info = new MBeanInfo(
|
||||
ComputerMBean.class.getSimpleName(),
|
||||
"metrics about all computers on the server",
|
||||
attributes.toArray( new MBeanAttributeInfo[0] ), null, null, null
|
||||
);
|
||||
}
|
||||
|
||||
public static void register()
|
||||
{
|
||||
try
|
||||
{
|
||||
ManagementFactory.getPlatformMBeanServer().registerMBean( instance = new ComputerMBean(), new ObjectName( "dan200.computercraft:type=Computers" ) );
|
||||
}
|
||||
catch( InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException | MalformedObjectNameException e )
|
||||
{
|
||||
ComputerCraft.log.warn( "Failed to register JMX bean", e );
|
||||
}
|
||||
}
|
||||
|
||||
public static void registerTracker()
|
||||
{
|
||||
if( instance != null ) Tracking.add( instance );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttribute( String attribute ) throws AttributeNotFoundException, MBeanException, ReflectionException
|
||||
{
|
||||
LongSupplier value = attributes.get( attribute );
|
||||
if( value == null ) throw new AttributeNotFoundException();
|
||||
return value.getAsLong();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute( Attribute attribute ) throws InvalidAttributeValueException
|
||||
{
|
||||
throw new InvalidAttributeValueException( "Cannot set attribute" );
|
||||
}
|
||||
|
||||
@Override
|
||||
public AttributeList getAttributes( String[] attributes )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AttributeList setAttributes( AttributeList attributes )
|
||||
{
|
||||
return new AttributeList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke( String actionName, Object[] params, String[] signature ) throws MBeanException, ReflectionException
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public MBeanInfo getMBeanInfo()
|
||||
{
|
||||
return info;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTaskTiming( Computer computer, long time )
|
||||
{
|
||||
addValue( computer, TrackingField.TOTAL_TIME, time );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addServerTiming( Computer computer, long time )
|
||||
{
|
||||
addValue( computer, TrackingField.SERVER_TIME, time );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addValue( Computer computer, TrackingField field, long change )
|
||||
{
|
||||
Counter counter = values.get( field );
|
||||
counter.value.addAndGet( change );
|
||||
counter.count.incrementAndGet();
|
||||
}
|
||||
|
||||
private MBeanAttributeInfo addAttribute( String name, String description, LongSupplier value )
|
||||
{
|
||||
attributes.put( name, value );
|
||||
return new MBeanAttributeInfo( name, "long", description, true, false, false );
|
||||
}
|
||||
|
||||
private void add( String name, TrackingField field, List<MBeanAttributeInfo> attributes, TrackingField count )
|
||||
{
|
||||
Counter counter = new Counter();
|
||||
values.put( field, counter );
|
||||
|
||||
String prettyName = LanguageMap.getInstance().getOrDefault( field.translationKey() );
|
||||
attributes.add( addAttribute( name, prettyName, counter.value::longValue ) );
|
||||
if( count != null )
|
||||
{
|
||||
String countName = LanguageMap.getInstance().getOrDefault( count.translationKey() );
|
||||
attributes.add( addAttribute( name + "Count", countName, counter.count::longValue ) );
|
||||
}
|
||||
}
|
||||
|
||||
private static class Counter
|
||||
{
|
||||
AtomicLong value = new AtomicLong();
|
||||
AtomicLong count = new AtomicLong();
|
||||
}
|
||||
}
|
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
package dan200.computercraft.data;
|
||||
|
||||
import dan200.computercraft.shared.proxy.ComputerCraftProxyCommon;
|
||||
import dan200.computercraft.shared.Registry;
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
@@ -17,7 +17,7 @@ public class Generators
|
||||
@SubscribeEvent
|
||||
public static void gather( GatherDataEvent event )
|
||||
{
|
||||
ComputerCraftProxyCommon.registerLoot();
|
||||
Registry.registerLoot();
|
||||
|
||||
DataGenerator generator = event.getGenerator();
|
||||
generator.addProvider( new Recipes( generator ) );
|
||||
|
@@ -6,11 +6,11 @@
|
||||
package dan200.computercraft.data;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.CommonHooks;
|
||||
import dan200.computercraft.shared.Registry;
|
||||
import dan200.computercraft.shared.data.BlockNamedEntityLootCondition;
|
||||
import dan200.computercraft.shared.data.HasComputerIdLootCondition;
|
||||
import dan200.computercraft.shared.data.PlayerCreativeLootCondition;
|
||||
import dan200.computercraft.shared.proxy.ComputerCraftProxyCommon;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraft.loot.*;
|
||||
@@ -46,7 +46,7 @@ public class LootTables extends LootTableProvider
|
||||
computerDrop( add, Registry.ModBlocks.TURTLE_NORMAL );
|
||||
computerDrop( add, Registry.ModBlocks.TURTLE_ADVANCED );
|
||||
|
||||
add.accept( ComputerCraftProxyCommon.ForgeHandlers.LOOT_TREASURE_DISK, LootTable
|
||||
add.accept( CommonHooks.LOOT_TREASURE_DISK, LootTable
|
||||
.lootTable()
|
||||
.setParamSet( LootParameterSets.ALL_PARAMS )
|
||||
.build() );
|
||||
|
@@ -13,20 +13,19 @@ import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public final class BundledRedstone
|
||||
{
|
||||
private static final Set<IBundledRedstoneProvider> providers = new LinkedHashSet<>();
|
||||
private static final ArrayList<IBundledRedstoneProvider> providers = new ArrayList<>();
|
||||
|
||||
private BundledRedstone() {}
|
||||
|
||||
public static synchronized void register( @Nonnull IBundledRedstoneProvider provider )
|
||||
{
|
||||
Objects.requireNonNull( provider, "provider cannot be null" );
|
||||
providers.add( provider );
|
||||
if( !providers.contains( provider ) ) providers.add( provider );
|
||||
}
|
||||
|
||||
public static int getDefaultOutput( @Nonnull World world, @Nonnull BlockPos pos, @Nonnull Direction side )
|
||||
|
138
src/main/java/dan200/computercraft/shared/CommonHooks.java
Normal file
138
src/main/java/dan200/computercraft/shared/CommonHooks.java
Normal file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.shared;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.core.computer.MainThread;
|
||||
import dan200.computercraft.core.tracking.ComputerMBean;
|
||||
import dan200.computercraft.core.tracking.Tracking;
|
||||
import dan200.computercraft.shared.command.CommandComputerCraft;
|
||||
import dan200.computercraft.shared.computer.core.IComputer;
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import dan200.computercraft.shared.peripheral.modem.wireless.WirelessNetwork;
|
||||
import net.minecraft.inventory.container.Container;
|
||||
import net.minecraft.loot.ConstantRange;
|
||||
import net.minecraft.loot.LootPool;
|
||||
import net.minecraft.loot.LootTables;
|
||||
import net.minecraft.loot.TableLootEntry;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.dedicated.DedicatedServer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.event.LootTableLoadEvent;
|
||||
import net.minecraftforge.event.RegisterCommandsEvent;
|
||||
import net.minecraftforge.event.TickEvent;
|
||||
import net.minecraftforge.event.entity.player.PlayerContainerEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.server.FMLServerStartedEvent;
|
||||
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
|
||||
import net.minecraftforge.fml.event.server.FMLServerStoppedEvent;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Miscellaneous hooks which are present on the client and server.
|
||||
*
|
||||
* These should possibly be refactored into separate classes at some point, but are fine here for now.
|
||||
*
|
||||
* @see dan200.computercraft.client.ClientHooks For client-specific ones.
|
||||
*/
|
||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID )
|
||||
public final class CommonHooks
|
||||
{
|
||||
private CommonHooks()
|
||||
{
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onServerTick( TickEvent.ServerTickEvent event )
|
||||
{
|
||||
if( event.phase == TickEvent.Phase.START )
|
||||
{
|
||||
MainThread.executePendingTasks();
|
||||
ComputerCraft.serverComputerRegistry.update();
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onContainerOpen( PlayerContainerEvent.Open event )
|
||||
{
|
||||
// If we're opening a computer container then broadcast the terminal state
|
||||
Container container = event.getContainer();
|
||||
if( container instanceof IContainerComputer )
|
||||
{
|
||||
IComputer computer = ((IContainerComputer) container).getComputer();
|
||||
if( computer instanceof ServerComputer )
|
||||
{
|
||||
((ServerComputer) computer).sendTerminalState( event.getPlayer() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onRegisterCommand( RegisterCommandsEvent event )
|
||||
{
|
||||
CommandComputerCraft.register( event.getDispatcher() );
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onServerStarting( FMLServerStartingEvent event )
|
||||
{
|
||||
MinecraftServer server = event.getServer();
|
||||
if( server instanceof DedicatedServer && ((DedicatedServer) server).getProperties().enableJmxMonitoring )
|
||||
{
|
||||
ComputerMBean.register();
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onServerStarted( FMLServerStartedEvent event )
|
||||
{
|
||||
ComputerCraft.serverComputerRegistry.reset();
|
||||
WirelessNetwork.resetNetworks();
|
||||
Tracking.reset();
|
||||
ComputerMBean.registerTracker();
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onServerStopped( FMLServerStoppedEvent event )
|
||||
{
|
||||
ComputerCraft.serverComputerRegistry.reset();
|
||||
WirelessNetwork.resetNetworks();
|
||||
Tracking.reset();
|
||||
}
|
||||
|
||||
public static final ResourceLocation LOOT_TREASURE_DISK = new ResourceLocation( ComputerCraft.MOD_ID, "treasure_disk" );
|
||||
|
||||
private static final Set<ResourceLocation> TABLES = new HashSet<>( Arrays.asList(
|
||||
LootTables.SIMPLE_DUNGEON,
|
||||
LootTables.ABANDONED_MINESHAFT,
|
||||
LootTables.STRONGHOLD_CORRIDOR,
|
||||
LootTables.STRONGHOLD_CROSSING,
|
||||
LootTables.STRONGHOLD_LIBRARY,
|
||||
LootTables.DESERT_PYRAMID,
|
||||
LootTables.JUNGLE_TEMPLE,
|
||||
LootTables.IGLOO_CHEST,
|
||||
LootTables.WOODLAND_MANSION,
|
||||
LootTables.VILLAGE_CARTOGRAPHER
|
||||
) );
|
||||
|
||||
@SubscribeEvent
|
||||
public static void lootLoad( LootTableLoadEvent event )
|
||||
{
|
||||
ResourceLocation name = event.getName();
|
||||
if( !name.getNamespace().equals( "minecraft" ) || !TABLES.contains( name ) ) return;
|
||||
|
||||
event.getTable().addPool( LootPool.lootPool()
|
||||
.add( TableLootEntry.lootTableReference( LOOT_TREASURE_DISK ) )
|
||||
.setRolls( ConstantRange.exactly( 1 ) )
|
||||
.name( "computercraft_treasure" )
|
||||
.build() );
|
||||
}
|
||||
}
|
@@ -42,7 +42,6 @@ public final class Config
|
||||
private static final ConfigValue<Integer> maximumFilesOpen;
|
||||
private static final ConfigValue<Boolean> disableLua51Features;
|
||||
private static final ConfigValue<String> defaultComputerSettings;
|
||||
private static final ConfigValue<Boolean> debugEnabled;
|
||||
private static final ConfigValue<Boolean> logComputerErrors;
|
||||
private static final ConfigValue<Boolean> commandRequireCreative;
|
||||
|
||||
@@ -120,10 +119,6 @@ public final class Config
|
||||
"autocompletion" )
|
||||
.define( "default_computer_settings", ComputerCraft.defaultComputerSettings );
|
||||
|
||||
debugEnabled = builder
|
||||
.comment( "Enable Lua's debug library. This is sandboxed to each computer, so is generally safe to be used by players." )
|
||||
.define( "debug_enabled", ComputerCraft.debugEnable );
|
||||
|
||||
logComputerErrors = builder
|
||||
.comment( "Log exceptions thrown by peripherals and other Lua objects.\n" +
|
||||
"This makes it easier for mod authors to debug problems, but may result in log spam should people use buggy methods." )
|
||||
@@ -317,7 +312,6 @@ public final class Config
|
||||
ComputerCraft.maximumFilesOpen = maximumFilesOpen.get();
|
||||
ComputerCraft.disableLua51Features = disableLua51Features.get();
|
||||
ComputerCraft.defaultComputerSettings = defaultComputerSettings.get();
|
||||
ComputerCraft.debugEnable = debugEnabled.get();
|
||||
ComputerCraft.computerThreads = computerThreads.get();
|
||||
ComputerCraft.logComputerErrors = logComputerErrors.get();
|
||||
ComputerCraft.commandRequireCreative = commandRequireCreative.get();
|
||||
|
@@ -7,8 +7,13 @@ package dan200.computercraft.shared;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.ComputerCraftAPI;
|
||||
import dan200.computercraft.api.media.IMedia;
|
||||
import dan200.computercraft.api.network.wired.IWiredElement;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.shared.command.arguments.ArgumentSerializers;
|
||||
import dan200.computercraft.shared.common.ColourableRecipe;
|
||||
import dan200.computercraft.shared.common.ContainerHeldItem;
|
||||
import dan200.computercraft.shared.common.DefaultBundledRedstoneProvider;
|
||||
import dan200.computercraft.shared.computer.blocks.BlockComputer;
|
||||
import dan200.computercraft.shared.computer.blocks.TileCommandComputer;
|
||||
import dan200.computercraft.shared.computer.blocks.TileComputer;
|
||||
@@ -17,11 +22,17 @@ import dan200.computercraft.shared.computer.inventory.ContainerComputer;
|
||||
import dan200.computercraft.shared.computer.inventory.ContainerViewComputer;
|
||||
import dan200.computercraft.shared.computer.items.ItemComputer;
|
||||
import dan200.computercraft.shared.computer.recipe.ComputerUpgradeRecipe;
|
||||
import dan200.computercraft.shared.data.BlockNamedEntityLootCondition;
|
||||
import dan200.computercraft.shared.data.HasComputerIdLootCondition;
|
||||
import dan200.computercraft.shared.data.PlayerCreativeLootCondition;
|
||||
import dan200.computercraft.shared.integration.morered.MoreRedIntegration;
|
||||
import dan200.computercraft.shared.media.items.ItemDisk;
|
||||
import dan200.computercraft.shared.media.items.ItemPrintout;
|
||||
import dan200.computercraft.shared.media.items.ItemTreasureDisk;
|
||||
import dan200.computercraft.shared.media.items.RecordMedia;
|
||||
import dan200.computercraft.shared.media.recipes.DiskRecipe;
|
||||
import dan200.computercraft.shared.media.recipes.PrintoutRecipe;
|
||||
import dan200.computercraft.shared.network.NetworkHandler;
|
||||
import dan200.computercraft.shared.network.container.ComputerContainerData;
|
||||
import dan200.computercraft.shared.network.container.ContainerData;
|
||||
import dan200.computercraft.shared.network.container.HeldItemContainerData;
|
||||
@@ -29,6 +40,9 @@ import dan200.computercraft.shared.network.container.ViewComputerContainerData;
|
||||
import dan200.computercraft.shared.peripheral.diskdrive.BlockDiskDrive;
|
||||
import dan200.computercraft.shared.peripheral.diskdrive.ContainerDiskDrive;
|
||||
import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive;
|
||||
import dan200.computercraft.shared.peripheral.generic.methods.EnergyMethods;
|
||||
import dan200.computercraft.shared.peripheral.generic.methods.FluidMethods;
|
||||
import dan200.computercraft.shared.peripheral.generic.methods.InventoryMethods;
|
||||
import dan200.computercraft.shared.peripheral.modem.wired.*;
|
||||
import dan200.computercraft.shared.peripheral.modem.wireless.BlockWirelessModem;
|
||||
import dan200.computercraft.shared.peripheral.modem.wireless.TileWirelessModem;
|
||||
@@ -52,30 +66,32 @@ import dan200.computercraft.shared.turtle.items.ItemTurtle;
|
||||
import dan200.computercraft.shared.turtle.recipes.TurtleRecipe;
|
||||
import dan200.computercraft.shared.turtle.recipes.TurtleUpgradeRecipe;
|
||||
import dan200.computercraft.shared.turtle.upgrades.*;
|
||||
import dan200.computercraft.shared.util.CreativeTabMain;
|
||||
import dan200.computercraft.shared.util.FixedPointTileEntityType;
|
||||
import dan200.computercraft.shared.util.ImpostorRecipe;
|
||||
import dan200.computercraft.shared.util.ImpostorShapelessRecipe;
|
||||
import dan200.computercraft.shared.util.*;
|
||||
import net.minecraft.block.AbstractBlock;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.entity.EntityClassification;
|
||||
import net.minecraft.entity.EntityType;
|
||||
import net.minecraft.inventory.container.ContainerType;
|
||||
import net.minecraft.item.BlockItem;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemGroup;
|
||||
import net.minecraft.item.Items;
|
||||
import net.minecraft.item.*;
|
||||
import net.minecraft.item.crafting.IRecipeSerializer;
|
||||
import net.minecraft.loot.LootConditionType;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.tileentity.TileEntityType;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.capabilities.CapabilityManager;
|
||||
import net.minecraftforge.energy.CapabilityEnergy;
|
||||
import net.minecraftforge.event.RegistryEvent;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
import net.minecraftforge.fml.DeferredWorkQueue;
|
||||
import net.minecraftforge.fml.ModList;
|
||||
import net.minecraftforge.fml.RegistryObject;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
@@ -334,6 +350,65 @@ public final class Registry
|
||||
);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
@SuppressWarnings( "deprecation" )
|
||||
public static void init( FMLCommonSetupEvent event )
|
||||
{
|
||||
NetworkHandler.setup();
|
||||
|
||||
DeferredWorkQueue.runLater( () -> {
|
||||
registerProviders();
|
||||
ArgumentSerializers.register();
|
||||
registerLoot();
|
||||
} );
|
||||
|
||||
ComputerCraftAPI.registerGenericSource( new InventoryMethods() );
|
||||
ComputerCraftAPI.registerGenericSource( new FluidMethods() );
|
||||
ComputerCraftAPI.registerGenericSource( new EnergyMethods() );
|
||||
}
|
||||
|
||||
private static void registerProviders()
|
||||
{
|
||||
// Register bundled power providers
|
||||
ComputerCraftAPI.registerBundledRedstoneProvider( new DefaultBundledRedstoneProvider() );
|
||||
|
||||
// Register media providers
|
||||
ComputerCraftAPI.registerMediaProvider( stack -> {
|
||||
Item item = stack.getItem();
|
||||
if( item instanceof IMedia ) return (IMedia) item;
|
||||
if( item instanceof MusicDiscItem ) return RecordMedia.INSTANCE;
|
||||
return null;
|
||||
} );
|
||||
|
||||
// Register capabilities
|
||||
CapabilityManager.INSTANCE.register( IWiredElement.class, new NullStorage<>(), () -> null );
|
||||
CapabilityManager.INSTANCE.register( IPeripheral.class, new NullStorage<>(), () -> null );
|
||||
|
||||
// Register generic capabilities. This can technically be done off-thread, but we need it to happen
|
||||
// after Forge's common setup, so this is easiest.
|
||||
ComputerCraftAPI.registerGenericCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY );
|
||||
ComputerCraftAPI.registerGenericCapability( CapabilityEnergy.ENERGY );
|
||||
ComputerCraftAPI.registerGenericCapability( CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY );
|
||||
|
||||
// Mod integration code.
|
||||
if( ModList.get().isLoaded( MoreRedIntegration.MOD_ID ) ) MoreRedIntegration.initialise();
|
||||
}
|
||||
|
||||
public static void registerLoot()
|
||||
{
|
||||
registerCondition( "block_named", BlockNamedEntityLootCondition.TYPE );
|
||||
registerCondition( "player_creative", PlayerCreativeLootCondition.TYPE );
|
||||
registerCondition( "has_id", HasComputerIdLootCondition.TYPE );
|
||||
}
|
||||
|
||||
private static void registerCondition( String name, LootConditionType serializer )
|
||||
{
|
||||
net.minecraft.util.registry.Registry.register(
|
||||
net.minecraft.util.registry.Registry.LOOT_CONDITION_TYPE,
|
||||
new ResourceLocation( ComputerCraft.MOD_ID, name ), serializer
|
||||
);
|
||||
}
|
||||
|
||||
public static void setup()
|
||||
{
|
||||
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||
|
@@ -33,9 +33,9 @@ public final class TurtleUpgrades
|
||||
Wrapper( ITurtleUpgrade upgrade )
|
||||
{
|
||||
this.upgrade = upgrade;
|
||||
this.id = upgrade.getUpgradeID().toString();
|
||||
this.modId = ModLoadingContext.get().getActiveNamespace();
|
||||
this.enabled = true;
|
||||
id = upgrade.getUpgradeID().toString();
|
||||
modId = ModLoadingContext.get().getActiveNamespace();
|
||||
enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.shared.command;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.util.IDAssigner;
|
||||
import net.minecraft.util.Util;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.client.event.ClientChatEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Basic client-side commands.
|
||||
*
|
||||
* Simply hooks into client chat messages and intercepts matching strings.
|
||||
*/
|
||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Dist.CLIENT )
|
||||
public final class ClientCommands
|
||||
{
|
||||
public static final String OPEN_COMPUTER = "/computercraft open-computer ";
|
||||
|
||||
private ClientCommands()
|
||||
{
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onClientSendMessage( ClientChatEvent event )
|
||||
{
|
||||
// Emulate the command on the client side
|
||||
if( event.getMessage().startsWith( OPEN_COMPUTER ) )
|
||||
{
|
||||
event.setCanceled( true );
|
||||
|
||||
String idStr = event.getMessage().substring( OPEN_COMPUTER.length() ).trim();
|
||||
int id;
|
||||
try
|
||||
{
|
||||
id = Integer.parseInt( idStr );
|
||||
}
|
||||
catch( NumberFormatException ignore )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
File file = new File( IDAssigner.getDir(), "computer/" + id );
|
||||
if( !file.isDirectory() ) return;
|
||||
|
||||
Util.getPlatform().openFile( file );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -21,6 +21,7 @@ import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import dan200.computercraft.shared.computer.inventory.ContainerViewComputer;
|
||||
import dan200.computercraft.shared.network.container.ViewComputerContainerData;
|
||||
import dan200.computercraft.shared.util.IDAssigner;
|
||||
import net.minecraft.command.CommandSource;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
@@ -38,6 +39,7 @@ import net.minecraft.world.World;
|
||||
import net.minecraft.world.server.ServerWorld;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
||||
import static dan200.computercraft.shared.command.CommandUtils.isPlayer;
|
||||
@@ -321,6 +323,12 @@ public final class CommandComputerCraft
|
||||
) );
|
||||
}
|
||||
|
||||
if( UserLevel.OWNER.test( source ) && isPlayer( source ) )
|
||||
{
|
||||
ITextComponent linkPath = linkStorage( computerId );
|
||||
if( linkPath != null ) out.append( " " ).append( linkPath );
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -340,6 +348,18 @@ public final class CommandComputerCraft
|
||||
}
|
||||
}
|
||||
|
||||
private static ITextComponent linkStorage( int id )
|
||||
{
|
||||
File file = new File( IDAssigner.getDir(), "computer/" + id );
|
||||
if( !file.isDirectory() ) return null;
|
||||
|
||||
return link(
|
||||
text( "\u270E" ),
|
||||
ClientCommands.OPEN_COMPUTER + id,
|
||||
translate( "commands.computercraft.dump.open_path" )
|
||||
);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private static TrackingContext getTimingContext( CommandSource source )
|
||||
{
|
||||
|
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.shared.command;
|
||||
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.command.CommandSource;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import net.minecraft.util.text.Style;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
import net.minecraft.util.text.event.ClickEvent;
|
||||
import net.minecraft.util.text.event.HoverEvent;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.client.event.ClientChatEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
|
||||
import static net.minecraft.command.Commands.argument;
|
||||
import static net.minecraft.command.Commands.literal;
|
||||
|
||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Dist.CLIENT )
|
||||
public final class CommandCopy
|
||||
{
|
||||
private static final String PREFIX = "/computercraft copy ";
|
||||
|
||||
private CommandCopy()
|
||||
{
|
||||
}
|
||||
|
||||
public static void register( CommandDispatcher<CommandSource> registry )
|
||||
{
|
||||
registry.register( literal( "computercraft" )
|
||||
.then( literal( "copy" ) )
|
||||
.then( argument( "message", StringArgumentType.greedyString() ) )
|
||||
.executes( context -> {
|
||||
Minecraft.getInstance().keyboardHandler.setClipboard( context.getArgument( "message", String.class ) );
|
||||
return 1;
|
||||
} )
|
||||
);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onClientSendMessage( ClientChatEvent event )
|
||||
{
|
||||
// Emulate the command on the client side
|
||||
if( event.getMessage().startsWith( PREFIX ) )
|
||||
{
|
||||
Minecraft.getInstance().keyboardHandler.setClipboard( event.getMessage().substring( PREFIX.length() ) );
|
||||
event.setCanceled( true );
|
||||
}
|
||||
}
|
||||
|
||||
public static ITextComponent createCopyText( String text )
|
||||
{
|
||||
return new StringTextComponent( text ).withStyle( Style.EMPTY
|
||||
.withClickEvent( new ClickEvent( ClickEvent.Action.RUN_COMMAND, PREFIX + text ) )
|
||||
.withHoverEvent( new HoverEvent( HoverEvent.Action.SHOW_TEXT, new TranslationTextComponent( "gui.computercraft.tooltip.copy" ) ) ) );
|
||||
}
|
||||
}
|
@@ -57,14 +57,15 @@ public enum UserLevel implements Predicate<CommandSource>
|
||||
{
|
||||
if( this == ANYONE ) return true;
|
||||
|
||||
// We *always* allow level 0 stuff, even if the
|
||||
MinecraftServer server = source.getServer();
|
||||
Entity sender = source.getEntity();
|
||||
|
||||
if( server.isSingleplayer() && sender instanceof PlayerEntity &&
|
||||
((PlayerEntity) sender).getGameProfile().getName().equalsIgnoreCase( server.getServerModName() ) )
|
||||
if( this == OWNER || this == OWNER_OP )
|
||||
{
|
||||
if( this == OWNER || this == OWNER_OP ) return true;
|
||||
MinecraftServer server = source.getServer();
|
||||
Entity sender = source.getEntity();
|
||||
if( server.isSingleplayer() && sender instanceof PlayerEntity &&
|
||||
((PlayerEntity) sender).getGameProfile().getName().equalsIgnoreCase( server.getServerModName() ) )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return source.hasPermission( toLevel() );
|
||||
|
@@ -68,19 +68,33 @@ public final class ChatHelpers
|
||||
: coloured( translate( "commands.computercraft.generic.no" ), TextFormatting.RED );
|
||||
}
|
||||
|
||||
public static IFormattableTextComponent link( IFormattableTextComponent component, String command, ITextComponent toolTip )
|
||||
public static ITextComponent link( IFormattableTextComponent component, String command, ITextComponent toolTip )
|
||||
{
|
||||
return link( component, new ClickEvent( ClickEvent.Action.RUN_COMMAND, command ), toolTip );
|
||||
}
|
||||
|
||||
public static ITextComponent link( ITextComponent component, ClickEvent click, ITextComponent toolTip )
|
||||
{
|
||||
Style style = component.getStyle();
|
||||
|
||||
if( style.getColor() == null ) style = style.withColor( TextFormatting.YELLOW );
|
||||
style = style.withClickEvent( new ClickEvent( ClickEvent.Action.RUN_COMMAND, command ) );
|
||||
style = style.withClickEvent( click );
|
||||
style = style.withHoverEvent( new HoverEvent( HoverEvent.Action.SHOW_TEXT, toolTip ) );
|
||||
|
||||
return component.setStyle( style );
|
||||
return component.copy().withStyle( style );
|
||||
}
|
||||
|
||||
public static IFormattableTextComponent header( String text )
|
||||
{
|
||||
return coloured( text, HEADER );
|
||||
}
|
||||
|
||||
public static IFormattableTextComponent copy( String text )
|
||||
{
|
||||
StringTextComponent name = new StringTextComponent( text );
|
||||
Style style = name.getStyle()
|
||||
.withClickEvent( new ClickEvent( ClickEvent.Action.COPY_TO_CLIPBOARD, text ) )
|
||||
.withHoverEvent( new HoverEvent( HoverEvent.Action.SHOW_TEXT, new TranslationTextComponent( "gui.computercraft.tooltip.copy" ) ) );
|
||||
return name.withStyle( style );
|
||||
}
|
||||
}
|
||||
|
@@ -61,7 +61,7 @@ public class ContainerHeldItem extends Container
|
||||
public Factory( ContainerType<ContainerHeldItem> type, ItemStack stack, Hand hand )
|
||||
{
|
||||
this.type = type;
|
||||
this.name = stack.getHoverName();
|
||||
name = stack.getHoverName();
|
||||
this.hand = hand;
|
||||
}
|
||||
|
||||
|
@@ -57,7 +57,7 @@ public class TileCommandComputer extends TileComputer
|
||||
@Override
|
||||
public boolean acceptsSuccess()
|
||||
{
|
||||
return getLevel().getGameRules().getBoolean( GameRules.RULE_SENDCOMMANDFEEDBACK );
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -175,12 +175,12 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
|
||||
label = computer.getLabel();
|
||||
on = computer.isOn();
|
||||
|
||||
if( computer.hasOutputChanged() ) updateOutput();
|
||||
|
||||
// Update the block state if needed. We don't fire a block update intentionally,
|
||||
// as this only really is needed on the client side.
|
||||
updateBlockState( computer.getState() );
|
||||
|
||||
// TODO: This should ideally be split up into label/id/on (which should save NBT and sync to client) and
|
||||
// redstone (which should update outputs)
|
||||
if( computer.hasOutputChanged() ) updateOutput();
|
||||
}
|
||||
}
|
||||
@@ -376,11 +376,8 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
|
||||
fresh = true;
|
||||
changed = true;
|
||||
}
|
||||
if( changed )
|
||||
{
|
||||
updateBlock();
|
||||
updateInput();
|
||||
}
|
||||
|
||||
if( changed ) updateInput();
|
||||
return ComputerCraft.serverComputerRegistry.get( instanceID );
|
||||
}
|
||||
|
||||
|
@@ -5,16 +5,18 @@
|
||||
*/
|
||||
package dan200.computercraft.shared.computer.core;
|
||||
|
||||
import dan200.computercraft.shared.computer.upload.FileUpload;
|
||||
import net.minecraft.entity.player.ServerPlayerEntity;
|
||||
import net.minecraft.inventory.container.Container;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An instance of {@link Container} which provides a computer. You should implement this
|
||||
* if you provide custom computers/GUIs to interact with them.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface IContainerComputer
|
||||
{
|
||||
/**
|
||||
@@ -33,8 +35,21 @@ public interface IContainerComputer
|
||||
* @return This container's input.
|
||||
*/
|
||||
@Nonnull
|
||||
default InputState getInput()
|
||||
{
|
||||
return new InputState( this );
|
||||
}
|
||||
InputState getInput();
|
||||
|
||||
/**
|
||||
* Attempt to upload a series of files to this computer.
|
||||
*
|
||||
* @param uploader The player uploading files.
|
||||
* @param files The files to upload.
|
||||
*/
|
||||
void upload( @Nonnull ServerPlayerEntity uploader, @Nonnull List<FileUpload> files );
|
||||
|
||||
/**
|
||||
* Continue an upload.
|
||||
*
|
||||
* @param uploader The player uploading files.
|
||||
* @param overwrite Whether the files should be overwritten or not.
|
||||
*/
|
||||
void continueUpload( @Nonnull ServerPlayerEntity uploader, boolean overwrite );
|
||||
}
|
||||
|
@@ -6,23 +6,41 @@
|
||||
package dan200.computercraft.shared.computer.inventory;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.core.filesystem.FileSystem;
|
||||
import dan200.computercraft.core.filesystem.FileSystemException;
|
||||
import dan200.computercraft.core.filesystem.FileSystemWrapper;
|
||||
import dan200.computercraft.shared.computer.core.*;
|
||||
import dan200.computercraft.shared.computer.upload.FileUpload;
|
||||
import dan200.computercraft.shared.computer.upload.UploadResult;
|
||||
import dan200.computercraft.shared.network.NetworkHandler;
|
||||
import dan200.computercraft.shared.network.client.UploadResultMessage;
|
||||
import dan200.computercraft.shared.network.container.ComputerContainerData;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.entity.player.ServerPlayerEntity;
|
||||
import net.minecraft.inventory.container.Container;
|
||||
import net.minecraft.inventory.container.ContainerType;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class ContainerComputerBase extends Container implements IContainerComputer
|
||||
{
|
||||
private static final String LIST_PREFIX = "\n \u2022 ";
|
||||
|
||||
private final Predicate<PlayerEntity> canUse;
|
||||
private final IComputer computer;
|
||||
private final ComputerFamily family;
|
||||
private final InputState input = new InputState( this );
|
||||
private List<FileUpload> toUpload;
|
||||
|
||||
protected ContainerComputerBase( ContainerType<? extends ContainerComputerBase> type, int id, Predicate<PlayerEntity> canUse, IComputer computer, ComputerFamily family )
|
||||
{
|
||||
@@ -73,6 +91,86 @@ public class ContainerComputerBase extends Container implements IContainerComput
|
||||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void upload( @Nonnull ServerPlayerEntity uploader, @Nonnull List<FileUpload> files )
|
||||
{
|
||||
UploadResultMessage message = upload( files, false );
|
||||
NetworkHandler.sendToPlayer( uploader, message );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void continueUpload( @Nonnull ServerPlayerEntity uploader, boolean overwrite )
|
||||
{
|
||||
List<FileUpload> files = this.toUpload;
|
||||
toUpload = null;
|
||||
if( files == null || files.isEmpty() || !overwrite ) return;
|
||||
|
||||
UploadResultMessage message = upload( files, true );
|
||||
NetworkHandler.sendToPlayer( uploader, message );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private UploadResultMessage upload( @Nonnull List<FileUpload> files, boolean forceOverwrite )
|
||||
{
|
||||
ServerComputer computer = (ServerComputer) getComputer();
|
||||
if( computer == null ) return UploadResultMessage.COMPUTER_OFF;
|
||||
|
||||
FileSystem fs = computer.getComputer().getEnvironment().getFileSystem();
|
||||
if( fs == null ) return UploadResultMessage.COMPUTER_OFF;
|
||||
|
||||
try
|
||||
{
|
||||
List<String> overwrite = new ArrayList<>();
|
||||
for( FileUpload upload : files )
|
||||
{
|
||||
if( !fs.exists( upload.getName() ) ) continue;
|
||||
if( fs.isDir( upload.getName() ) )
|
||||
{
|
||||
return new UploadResultMessage(
|
||||
UploadResult.ERROR,
|
||||
new TranslationTextComponent( "gui.computercraft.upload.failed.overwrite_dir", upload.getName() )
|
||||
);
|
||||
}
|
||||
|
||||
overwrite.add( upload.getName() );
|
||||
}
|
||||
|
||||
if( !overwrite.isEmpty() && !forceOverwrite )
|
||||
{
|
||||
StringJoiner joiner = new StringJoiner( LIST_PREFIX, LIST_PREFIX, "" );
|
||||
for( String value : overwrite ) joiner.add( value );
|
||||
|
||||
toUpload = files;
|
||||
return new UploadResultMessage(
|
||||
UploadResult.CONFIRM_OVERWRITE,
|
||||
new TranslationTextComponent( "gui.computercraft.upload.overwrite.detail", joiner.toString() )
|
||||
);
|
||||
}
|
||||
|
||||
long availableSpace = fs.getFreeSpace( "/" );
|
||||
long neededSpace = 0;
|
||||
for( FileUpload upload : files ) neededSpace += Math.max( 512, upload.getBytes().remaining() );
|
||||
if( neededSpace > availableSpace ) return UploadResultMessage.OUT_OF_SPACE;
|
||||
|
||||
for( FileUpload file : files )
|
||||
{
|
||||
try( FileSystemWrapper<WritableByteChannel> channel = fs.openForWrite( file.getName(), false, Function.identity() ) )
|
||||
{
|
||||
channel.get().write( file.getBytes() );
|
||||
}
|
||||
}
|
||||
|
||||
return new UploadResultMessage(
|
||||
UploadResult.SUCCESS, new TranslationTextComponent( "gui.computercraft.upload.success.msg", files.size() )
|
||||
);
|
||||
}
|
||||
catch( FileSystemException | IOException e )
|
||||
{
|
||||
ComputerCraft.log.error( "Error uploading files", e );
|
||||
return new UploadResultMessage( UploadResult.ERROR, new TranslationTextComponent( "computercraft.gui.upload.failed.generic", e.getMessage() ) );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removed( @Nonnull PlayerEntity player )
|
||||
{
|
||||
|
@@ -9,7 +9,6 @@ import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.Registry;
|
||||
import dan200.computercraft.shared.computer.blocks.TileCommandComputer;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import dan200.computercraft.shared.network.container.ViewComputerContainerData;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
@@ -17,7 +16,7 @@ import net.minecraft.entity.player.PlayerInventory;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class ContainerViewComputer extends ContainerComputerBase implements IContainerComputer
|
||||
public class ContainerViewComputer extends ContainerComputerBase
|
||||
{
|
||||
private final int width;
|
||||
private final int height;
|
||||
@@ -25,14 +24,14 @@ public class ContainerViewComputer extends ContainerComputerBase implements ICon
|
||||
public ContainerViewComputer( int id, ServerComputer computer )
|
||||
{
|
||||
super( Registry.ModContainers.VIEW_COMPUTER.get(), id, player -> canInteractWith( computer, player ), computer, computer.getFamily() );
|
||||
this.width = this.height = 0;
|
||||
width = height = 0;
|
||||
}
|
||||
|
||||
public ContainerViewComputer( int id, PlayerInventory player, ViewComputerContainerData data )
|
||||
{
|
||||
super( Registry.ModContainers.VIEW_COMPUTER.get(), id, player, data );
|
||||
this.width = data.getWidth();
|
||||
this.height = data.getHeight();
|
||||
width = data.getWidth();
|
||||
height = data.getHeight();
|
||||
}
|
||||
|
||||
private static boolean canInteractWith( @Nonnull ServerComputer computer, @Nonnull PlayerEntity player )
|
||||
|
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.shared.computer.upload;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class FileUpload
|
||||
{
|
||||
private final String name;
|
||||
private final ByteBuffer bytes;
|
||||
|
||||
public FileUpload( String name, ByteBuffer bytes )
|
||||
{
|
||||
this.name = name;
|
||||
this.bytes = bytes;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public ByteBuffer getBytes()
|
||||
{
|
||||
return bytes;
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.shared.computer.upload;
|
||||
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
|
||||
public enum UploadResult
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR,
|
||||
CONFIRM_OVERWRITE;
|
||||
|
||||
public static final ITextComponent SUCCESS_TITLE = new TranslationTextComponent( "gui.computercraft.upload.success" );
|
||||
|
||||
public static final ITextComponent FAILED_TITLE = new TranslationTextComponent( "gui.computercraft.upload.failed" );
|
||||
public static final ITextComponent COMPUTER_OFF_MSG = new TranslationTextComponent( "gui.computercraft.upload.failed.computer_off" );
|
||||
public static final ITextComponent OUT_OF_SPACE_MSG = new TranslationTextComponent( "gui.computercraft.upload.failed.out_of_space" );
|
||||
public static final ITextComponent TOO_MUCH_MSG = new TranslationTextComponent( "gui.computercraft.upload.failed.too_much" );
|
||||
|
||||
public static final ITextComponent UPLOAD_OVERWRITE = new TranslationTextComponent( "gui.computercraft.upload.overwrite" );
|
||||
}
|
@@ -99,13 +99,17 @@ public class AddTurtleTool implements IUndoableAction
|
||||
return String.format( "Removing turtle upgrade %s.", id );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate( ILogger logger )
|
||||
{
|
||||
TrackingLogger trackLog = new TrackingLogger( logger );
|
||||
|
||||
if( craftItem.isEmpty() ) trackLog.error( "Crafting item stack is empty." );
|
||||
|
||||
if( craftItem.hasTag() && !craftItem.getTag().isEmpty() ) trackLog.warning( "Crafting item has NBT." );
|
||||
if( craftItem.isDamaged() || craftItem.isEnchanted() || craftItem.hasCustomHoverName() )
|
||||
{
|
||||
trackLog.warning( "Crafting item has NBT." );
|
||||
}
|
||||
if( toolItem.isEmpty() ) trackLog.error( "Tool item stack is empty." );
|
||||
|
||||
if( !kinds.containsKey( kind ) ) trackLog.error( String.format( "Unknown kind '%s'.", kind ) );
|
||||
@@ -121,6 +125,6 @@ public class AddTurtleTool implements IUndoableAction
|
||||
@Override
|
||||
public boolean shouldApplyOn( LogicalSide side )
|
||||
{
|
||||
return true;
|
||||
return shouldApplySingletons();
|
||||
}
|
||||
}
|
||||
|
@@ -41,7 +41,7 @@ public class RemoveTurtleUpgradeByItem implements IUndoableAction
|
||||
@Override
|
||||
public void undo()
|
||||
{
|
||||
if( this.upgrade != null ) TurtleUpgrades.enable( upgrade );
|
||||
if( upgrade != null ) TurtleUpgrades.enable( upgrade );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -40,7 +40,7 @@ public class RemoveTurtleUpgradeByName implements IUndoableAction
|
||||
@Override
|
||||
public void undo()
|
||||
{
|
||||
if( this.upgrade != null ) TurtleUpgrades.enable( upgrade );
|
||||
if( upgrade != null ) TurtleUpgrades.enable( upgrade );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -22,7 +22,7 @@ import mezz.jei.api.IModPlugin;
|
||||
import mezz.jei.api.JeiPlugin;
|
||||
import mezz.jei.api.constants.VanillaRecipeCategoryUid;
|
||||
import mezz.jei.api.constants.VanillaTypes;
|
||||
import mezz.jei.api.ingredients.subtypes.ISubtypeInterpreter;
|
||||
import mezz.jei.api.ingredients.subtypes.IIngredientSubtypeInterpreter;
|
||||
import mezz.jei.api.recipe.IRecipeManager;
|
||||
import mezz.jei.api.recipe.category.IRecipeCategory;
|
||||
import mezz.jei.api.registration.IAdvancedRegistration;
|
||||
@@ -90,13 +90,13 @@ public class JEIComputerCraft implements IModPlugin
|
||||
runtime.getIngredientManager().addIngredientsAtRuntime( VanillaTypes.ITEM, upgradeItems );
|
||||
|
||||
// Hide all upgrade recipes
|
||||
IRecipeCategory<?> category = (IRecipeCategory<?>) registry.getRecipeCategory( VanillaRecipeCategoryUid.CRAFTING );
|
||||
IRecipeCategory<?> category = registry.getRecipeCategory( VanillaRecipeCategoryUid.CRAFTING );
|
||||
if( category != null )
|
||||
{
|
||||
for( Object wrapper : registry.getRecipes( category ) )
|
||||
{
|
||||
if( !(wrapper instanceof IRecipe) ) continue;
|
||||
ResourceLocation id = ((IRecipe) wrapper).getId();
|
||||
ResourceLocation id = ((IRecipe<?>) wrapper).getId();
|
||||
if( !id.getNamespace().equals( ComputerCraft.MOD_ID ) ) continue;
|
||||
|
||||
String path = id.getPath();
|
||||
@@ -112,9 +112,9 @@ public class JEIComputerCraft implements IModPlugin
|
||||
/**
|
||||
* Distinguishes turtles by upgrades and family.
|
||||
*/
|
||||
private static final ISubtypeInterpreter turtleSubtype = stack -> {
|
||||
private static final IIngredientSubtypeInterpreter<ItemStack> turtleSubtype = ( stack, ctx ) -> {
|
||||
Item item = stack.getItem();
|
||||
if( !(item instanceof ITurtleItem) ) return ISubtypeInterpreter.NONE;
|
||||
if( !(item instanceof ITurtleItem) ) return IIngredientSubtypeInterpreter.NONE;
|
||||
|
||||
ITurtleItem turtle = (ITurtleItem) item;
|
||||
StringBuilder name = new StringBuilder( "turtle:" );
|
||||
@@ -132,9 +132,9 @@ public class JEIComputerCraft implements IModPlugin
|
||||
/**
|
||||
* Distinguishes pocket computers by upgrade and family.
|
||||
*/
|
||||
private static final ISubtypeInterpreter pocketSubtype = stack -> {
|
||||
private static final IIngredientSubtypeInterpreter<ItemStack> pocketSubtype = ( stack, ctx ) -> {
|
||||
Item item = stack.getItem();
|
||||
if( !(item instanceof ItemPocketComputer) ) return ISubtypeInterpreter.NONE;
|
||||
if( !(item instanceof ItemPocketComputer) ) return IIngredientSubtypeInterpreter.NONE;
|
||||
|
||||
StringBuilder name = new StringBuilder( "pocket:" );
|
||||
|
||||
@@ -148,13 +148,13 @@ public class JEIComputerCraft implements IModPlugin
|
||||
/**
|
||||
* Distinguishes disks by colour.
|
||||
*/
|
||||
private static final ISubtypeInterpreter diskSubtype = stack -> {
|
||||
private static final IIngredientSubtypeInterpreter<ItemStack> diskSubtype = ( stack, ctx ) -> {
|
||||
Item item = stack.getItem();
|
||||
if( !(item instanceof ItemDisk) ) return ISubtypeInterpreter.NONE;
|
||||
if( !(item instanceof ItemDisk) ) return IIngredientSubtypeInterpreter.NONE;
|
||||
|
||||
ItemDisk disk = (ItemDisk) item;
|
||||
|
||||
int colour = disk.getColour( stack );
|
||||
return colour == -1 ? ISubtypeInterpreter.NONE : String.format( "%06x", colour );
|
||||
return colour == -1 ? IIngredientSubtypeInterpreter.NONE : String.format( "%06x", colour );
|
||||
};
|
||||
}
|
||||
|
@@ -339,17 +339,17 @@ class RecipeResolver implements IRecipeManagerPlugin
|
||||
UpgradeInfo( ItemStack stack, ITurtleUpgrade turtle )
|
||||
{
|
||||
this.stack = stack;
|
||||
this.ingredient = of( stack );
|
||||
this.upgrade = this.turtle = turtle;
|
||||
this.pocket = null;
|
||||
ingredient = of( stack );
|
||||
upgrade = this.turtle = turtle;
|
||||
pocket = null;
|
||||
}
|
||||
|
||||
UpgradeInfo( ItemStack stack, IPocketUpgrade pocket )
|
||||
{
|
||||
this.stack = stack;
|
||||
this.ingredient = of( stack );
|
||||
this.turtle = null;
|
||||
this.upgrade = this.pocket = pocket;
|
||||
ingredient = of( stack );
|
||||
turtle = null;
|
||||
upgrade = this.pocket = pocket;
|
||||
}
|
||||
|
||||
List<Shaped> getRecipes()
|
||||
|
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.shared.integration.morered;
|
||||
|
||||
import commoble.morered.api.ChanneledPowerSupplier;
|
||||
import commoble.morered.api.MoreRedAPI;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.ComputerCraftAPI;
|
||||
import dan200.computercraft.shared.common.IBundledRedstoneBlock;
|
||||
import dan200.computercraft.shared.util.CapabilityUtil;
|
||||
import dan200.computercraft.shared.util.FixedPointTileEntityType;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.Direction;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.event.AttachCapabilitiesEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class MoreRedIntegration
|
||||
{
|
||||
public static final String MOD_ID = "morered";
|
||||
|
||||
private static final ResourceLocation ID = new ResourceLocation( ComputerCraft.MOD_ID, "morered" );
|
||||
|
||||
private static final class BundledPowerSupplier implements ICapabilityProvider, ChanneledPowerSupplier
|
||||
{
|
||||
private final IBundledRedstoneBlock block;
|
||||
private final TileEntity tile;
|
||||
private LazyOptional<ChanneledPowerSupplier> instance;
|
||||
|
||||
private BundledPowerSupplier( IBundledRedstoneBlock block, TileEntity tile )
|
||||
{
|
||||
this.block = block;
|
||||
this.tile = tile;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public <T> LazyOptional<T> getCapability( @Nonnull Capability<T> cap, @Nullable Direction side )
|
||||
{
|
||||
if( cap != MoreRedAPI.CHANNELED_POWER_CAPABILITY ) return LazyOptional.empty();
|
||||
|
||||
if( tile.isRemoved() || !block.getBundledRedstoneConnectivity( tile.getLevel(), tile.getBlockPos(), side ) )
|
||||
{
|
||||
return LazyOptional.empty();
|
||||
}
|
||||
|
||||
return (instance == null ? (instance = LazyOptional.of( () -> this )) : instance).cast();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPowerOnChannel( @Nonnull World world, @Nonnull BlockPos wirePos, @Nonnull BlockState wireState, @Nullable Direction wireFace, int channel )
|
||||
{
|
||||
if( wireFace == null ) return 0;
|
||||
|
||||
BlockPos pos = wirePos.relative( wireFace );
|
||||
BlockState state = world.getBlockState( pos );
|
||||
if( !(state.getBlock() instanceof IBundledRedstoneBlock) ) return 0;
|
||||
|
||||
IBundledRedstoneBlock block = (IBundledRedstoneBlock) state.getBlock();
|
||||
return (block.getBundledRedstoneOutput( world, pos, wireFace.getOpposite() ) & (1 << channel)) != 0 ? 31 : 0;
|
||||
}
|
||||
|
||||
void invalidate()
|
||||
{
|
||||
instance = CapabilityUtil.invalidate( instance );
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void attachBlockCapabilities( AttachCapabilitiesEvent<TileEntity> event )
|
||||
{
|
||||
TileEntity tile = event.getObject();
|
||||
if( !(tile.getType() instanceof FixedPointTileEntityType) ) return;
|
||||
|
||||
Block block = ((FixedPointTileEntityType<?>) tile.getType()).getBlock();
|
||||
if( !(block instanceof IBundledRedstoneBlock) ) return;
|
||||
|
||||
BundledPowerSupplier provider = new BundledPowerSupplier( (IBundledRedstoneBlock) block, tile );
|
||||
event.addCapability( ID, provider );
|
||||
event.addListener( provider::invalidate );
|
||||
}
|
||||
|
||||
public static void initialise()
|
||||
{
|
||||
MinecraftForge.EVENT_BUS.register( MoreRedIntegration.class );
|
||||
ComputerCraftAPI.registerBundledRedstoneProvider( MoreRedIntegration::getBundledPower );
|
||||
}
|
||||
|
||||
private static int getBundledPower( World world, BlockPos pos, Direction side )
|
||||
{
|
||||
TileEntity tile = world.getBlockEntity( pos );
|
||||
if( tile == null ) return -1;
|
||||
|
||||
ChanneledPowerSupplier power = CapabilityUtil.unwrapUnsafe( tile.getCapability( MoreRedAPI.CHANNELED_POWER_CAPABILITY, side ) );
|
||||
if( power == null ) return -1;
|
||||
|
||||
BlockState state = tile.getBlockState();
|
||||
|
||||
// Skip ones already handled by CC. We can do this more efficiently.
|
||||
if( state.getBlock() instanceof IBundledRedstoneBlock ) return -1;
|
||||
|
||||
int mask = 0;
|
||||
for( int i = 0; i < 16; i++ )
|
||||
{
|
||||
mask |= power.getPowerOnChannel( world, pos, state, side, i ) > 0 ? (1 << i) : 0;
|
||||
}
|
||||
|
||||
return mask;
|
||||
}
|
||||
}
|
@@ -68,6 +68,8 @@ public class ItemTreasureDisk extends Item implements IMedia
|
||||
public IMount createDataMount( @Nonnull ItemStack stack, @Nonnull World world )
|
||||
{
|
||||
IMount rootTreasure = getTreasureMount();
|
||||
if( rootTreasure == null ) return null;
|
||||
|
||||
String subPath = getSubPath( stack );
|
||||
try
|
||||
{
|
||||
@@ -121,7 +123,7 @@ public class ItemTreasureDisk extends Item implements IMedia
|
||||
private static String getTitle( @Nonnull ItemStack stack )
|
||||
{
|
||||
CompoundNBT nbt = stack.getTag();
|
||||
return nbt != null && nbt.contains( NBT_TITLE ) ? nbt.getString( NBT_TITLE ) : "'alongtimeago' by dan200";
|
||||
return nbt != null && nbt.contains( NBT_TITLE ) ? nbt.getString( NBT_TITLE ) : "'missingno' by how did you get this anyway?";
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
|
@@ -43,19 +43,25 @@ public final class NetworkHandler
|
||||
.simpleChannel();
|
||||
|
||||
// Server messages
|
||||
registerMainThread( 0, NetworkDirection.PLAY_TO_SERVER, ComputerActionServerMessage::new );
|
||||
registerMainThread( 1, NetworkDirection.PLAY_TO_SERVER, QueueEventServerMessage::new );
|
||||
registerMainThread( 2, NetworkDirection.PLAY_TO_SERVER, RequestComputerMessage::new );
|
||||
registerMainThread( 3, NetworkDirection.PLAY_TO_SERVER, KeyEventServerMessage::new );
|
||||
registerMainThread( 4, NetworkDirection.PLAY_TO_SERVER, MouseEventServerMessage::new );
|
||||
registerMainThread( 0, NetworkDirection.PLAY_TO_SERVER, ComputerActionServerMessage.class, ComputerActionServerMessage::new );
|
||||
registerMainThread( 1, NetworkDirection.PLAY_TO_SERVER, QueueEventServerMessage.class, QueueEventServerMessage::new );
|
||||
registerMainThread( 2, NetworkDirection.PLAY_TO_SERVER, RequestComputerMessage.class, RequestComputerMessage::new );
|
||||
registerMainThread( 3, NetworkDirection.PLAY_TO_SERVER, KeyEventServerMessage.class, KeyEventServerMessage::new );
|
||||
registerMainThread( 4, NetworkDirection.PLAY_TO_SERVER, MouseEventServerMessage.class, MouseEventServerMessage::new );
|
||||
registerMainThread( 5, NetworkDirection.PLAY_TO_SERVER, UploadFileMessage.class, UploadFileMessage::new );
|
||||
registerMainThread( 6, NetworkDirection.PLAY_TO_SERVER, ContinueUploadMessage.class, ContinueUploadMessage::new );
|
||||
|
||||
// Client messages
|
||||
registerMainThread( 10, NetworkDirection.PLAY_TO_CLIENT, ChatTableClientMessage::new );
|
||||
registerMainThread( 11, NetworkDirection.PLAY_TO_CLIENT, ComputerDataClientMessage::new );
|
||||
registerMainThread( 12, NetworkDirection.PLAY_TO_CLIENT, ComputerDeletedClientMessage::new );
|
||||
registerMainThread( 13, NetworkDirection.PLAY_TO_CLIENT, ComputerTerminalClientMessage::new );
|
||||
registerMainThread( 10, NetworkDirection.PLAY_TO_CLIENT, ChatTableClientMessage.class, ChatTableClientMessage::new );
|
||||
registerMainThread( 11, NetworkDirection.PLAY_TO_CLIENT, ComputerDataClientMessage.class, ComputerDataClientMessage::new );
|
||||
registerMainThread( 12, NetworkDirection.PLAY_TO_CLIENT, ComputerDeletedClientMessage.class, ComputerDeletedClientMessage::new );
|
||||
registerMainThread( 13, NetworkDirection.PLAY_TO_CLIENT, ComputerTerminalClientMessage.class, ComputerTerminalClientMessage::new );
|
||||
registerMainThread( 14, NetworkDirection.PLAY_TO_CLIENT, PlayRecordClientMessage.class, PlayRecordClientMessage::new );
|
||||
registerMainThread( 15, NetworkDirection.PLAY_TO_CLIENT, MonitorClientMessage.class, MonitorClientMessage::new );
|
||||
registerMainThread( 16, NetworkDirection.PLAY_TO_CLIENT, SpeakerPlayClientMessage.class, SpeakerPlayClientMessage::new );
|
||||
registerMainThread( 17, NetworkDirection.PLAY_TO_CLIENT, SpeakerStopClientMessage.class, SpeakerStopClientMessage::new );
|
||||
registerMainThread( 18, NetworkDirection.PLAY_TO_CLIENT, SpeakerMoveClientMessage.class, SpeakerMoveClientMessage::new );
|
||||
registerMainThread( 19, NetworkDirection.PLAY_TO_CLIENT, UploadResultMessage.class, UploadResultMessage::new );
|
||||
}
|
||||
|
||||
public static void sendToPlayer( PlayerEntity player, NetworkMessage packet )
|
||||
@@ -87,24 +93,6 @@ public final class NetworkHandler
|
||||
network.send( PacketDistributor.TRACKING_CHUNK.with( () -> chunk ), packet );
|
||||
}
|
||||
|
||||
/**
|
||||
* /**
|
||||
* Register packet, and a thread-unsafe handler for it.
|
||||
*
|
||||
* @param <T> The type of the packet to send.
|
||||
* @param id The identifier for this packet type.
|
||||
* @param direction A network direction which will be asserted before any processing of this message occurs.
|
||||
* @param factory The factory for this type of packet.
|
||||
*/
|
||||
private static <T extends NetworkMessage> void registerMainThread( int id, NetworkDirection direction, Supplier<T> factory )
|
||||
{
|
||||
registerMainThread( id, direction, getType( factory ), buf -> {
|
||||
T instance = factory.get();
|
||||
instance.fromBytes( buf );
|
||||
return instance;
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* /**
|
||||
* Register packet, and a thread-unsafe handler for it.
|
||||
|
@@ -27,18 +27,6 @@ public interface NetworkMessage
|
||||
*/
|
||||
void toBytes( @Nonnull PacketBuffer buf );
|
||||
|
||||
/**
|
||||
* Read this packet from a buffer.
|
||||
*
|
||||
* This may be called on any thread, so this should be a pure operation.
|
||||
*
|
||||
* @param buf The buffer to read data from.
|
||||
*/
|
||||
default void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
throw new IllegalStateException( "Should have been registered using a \"from bytes\" method" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle this {@link NetworkMessage}.
|
||||
*
|
||||
|
@@ -16,7 +16,7 @@ import javax.annotation.Nonnull;
|
||||
|
||||
public class ChatTableClientMessage implements NetworkMessage
|
||||
{
|
||||
private TableBuilder table;
|
||||
private final TableBuilder table;
|
||||
|
||||
public ChatTableClientMessage( TableBuilder table )
|
||||
{
|
||||
@@ -24,32 +24,7 @@ public class ChatTableClientMessage implements NetworkMessage
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
public ChatTableClientMessage()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
buf.writeVarInt( table.getId() );
|
||||
buf.writeVarInt( table.getColumns() );
|
||||
buf.writeBoolean( table.getHeaders() != null );
|
||||
if( table.getHeaders() != null )
|
||||
{
|
||||
for( ITextComponent header : table.getHeaders() ) buf.writeComponent( header );
|
||||
}
|
||||
|
||||
buf.writeVarInt( table.getRows().size() );
|
||||
for( ITextComponent[] row : table.getRows() )
|
||||
{
|
||||
for( ITextComponent column : row ) buf.writeComponent( column );
|
||||
}
|
||||
|
||||
buf.writeVarInt( table.getAdditional() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
public ChatTableClientMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
int id = buf.readVarInt();
|
||||
int columns = buf.readVarInt();
|
||||
@@ -77,6 +52,26 @@ public class ChatTableClientMessage implements NetworkMessage
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
buf.writeVarInt( table.getId() );
|
||||
buf.writeVarInt( table.getColumns() );
|
||||
buf.writeBoolean( table.getHeaders() != null );
|
||||
if( table.getHeaders() != null )
|
||||
{
|
||||
for( ITextComponent header : table.getHeaders() ) buf.writeComponent( header );
|
||||
}
|
||||
|
||||
buf.writeVarInt( table.getRows().size() );
|
||||
for( ITextComponent[] row : table.getRows() )
|
||||
{
|
||||
for( ITextComponent column : row ) buf.writeComponent( column );
|
||||
}
|
||||
|
||||
buf.writeVarInt( table.getAdditional() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
|
@@ -17,15 +17,16 @@ import javax.annotation.Nonnull;
|
||||
*/
|
||||
public abstract class ComputerClientMessage implements NetworkMessage
|
||||
{
|
||||
private int instanceId;
|
||||
private final int instanceId;
|
||||
|
||||
public ComputerClientMessage( int instanceId )
|
||||
{
|
||||
this.instanceId = instanceId;
|
||||
}
|
||||
|
||||
public ComputerClientMessage()
|
||||
public ComputerClientMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
instanceId = buf.readVarInt();
|
||||
}
|
||||
|
||||
public int getInstanceId()
|
||||
@@ -39,12 +40,6 @@ public abstract class ComputerClientMessage implements NetworkMessage
|
||||
buf.writeVarInt( instanceId );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
instanceId = buf.readVarInt();
|
||||
}
|
||||
|
||||
public ClientComputer getComputer()
|
||||
{
|
||||
ClientComputer computer = ComputerCraft.clientComputerRegistry.get( instanceId );
|
||||
|
@@ -18,8 +18,8 @@ import javax.annotation.Nonnull;
|
||||
*/
|
||||
public class ComputerDataClientMessage extends ComputerClientMessage
|
||||
{
|
||||
private ComputerState state;
|
||||
private CompoundNBT userData;
|
||||
private final ComputerState state;
|
||||
private final CompoundNBT userData;
|
||||
|
||||
public ComputerDataClientMessage( ServerComputer computer )
|
||||
{
|
||||
@@ -28,8 +28,11 @@ public class ComputerDataClientMessage extends ComputerClientMessage
|
||||
userData = computer.getUserData();
|
||||
}
|
||||
|
||||
public ComputerDataClientMessage()
|
||||
public ComputerDataClientMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
state = buf.readEnum( ComputerState.class );
|
||||
userData = buf.readNbt();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -40,14 +43,6 @@ public class ComputerDataClientMessage extends ComputerClientMessage
|
||||
buf.writeNbt( userData );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
state = buf.readEnum( ComputerState.class );
|
||||
userData = buf.readNbt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
|
@@ -6,6 +6,7 @@
|
||||
package dan200.computercraft.shared.network.client;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
|
||||
public class ComputerDeletedClientMessage extends ComputerClientMessage
|
||||
@@ -15,8 +16,9 @@ public class ComputerDeletedClientMessage extends ComputerClientMessage
|
||||
super( instanceId );
|
||||
}
|
||||
|
||||
public ComputerDeletedClientMessage()
|
||||
public ComputerDeletedClientMessage( PacketBuffer buffer )
|
||||
{
|
||||
super( buffer );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -12,7 +12,7 @@ import javax.annotation.Nonnull;
|
||||
|
||||
public class ComputerTerminalClientMessage extends ComputerClientMessage
|
||||
{
|
||||
private TerminalState state;
|
||||
private final TerminalState state;
|
||||
|
||||
public ComputerTerminalClientMessage( int instanceId, TerminalState state )
|
||||
{
|
||||
@@ -20,8 +20,10 @@ public class ComputerTerminalClientMessage extends ComputerClientMessage
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public ComputerTerminalClientMessage()
|
||||
public ComputerTerminalClientMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
state = new TerminalState( buf );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,13 +33,6 @@ public class ComputerTerminalClientMessage extends ComputerClientMessage
|
||||
state.write( buf );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
state = new TerminalState( buf );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
|
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.shared.network.client;
|
||||
|
||||
import dan200.computercraft.client.SoundManager;
|
||||
import dan200.computercraft.shared.network.NetworkMessage;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.math.vector.Vector3d;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Starts a sound on the client.
|
||||
*
|
||||
* Used by speakers to play sounds.
|
||||
*
|
||||
* @see dan200.computercraft.shared.peripheral.speaker.TileSpeaker
|
||||
*/
|
||||
public class SpeakerMoveClientMessage implements NetworkMessage
|
||||
{
|
||||
private final UUID source;
|
||||
private final Vector3d pos;
|
||||
|
||||
public SpeakerMoveClientMessage( UUID source, Vector3d pos )
|
||||
{
|
||||
this.source = source;
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
public SpeakerMoveClientMessage( PacketBuffer buf )
|
||||
{
|
||||
source = buf.readUUID();
|
||||
pos = new Vector3d( buf.readDouble(), buf.readDouble(), buf.readDouble() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
buf.writeUUID( source );
|
||||
buf.writeDouble( pos.x() );
|
||||
buf.writeDouble( pos.y() );
|
||||
buf.writeDouble( pos.z() );
|
||||
}
|
||||
|
||||
@Override
|
||||
@OnlyIn( Dist.CLIENT )
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
SoundManager.moveSound( source, pos );
|
||||
}
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.shared.network.client;
|
||||
|
||||
import dan200.computercraft.client.SoundManager;
|
||||
import dan200.computercraft.shared.network.NetworkMessage;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.util.math.vector.Vector3d;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Starts a sound on the client.
|
||||
*
|
||||
* Used by speakers to play sounds.
|
||||
*
|
||||
* @see dan200.computercraft.shared.peripheral.speaker.TileSpeaker
|
||||
*/
|
||||
public class SpeakerPlayClientMessage implements NetworkMessage
|
||||
{
|
||||
private final UUID source;
|
||||
private final Vector3d pos;
|
||||
private final ResourceLocation sound;
|
||||
private final float volume;
|
||||
private final float pitch;
|
||||
|
||||
public SpeakerPlayClientMessage( UUID source, Vector3d pos, ResourceLocation event, float volume, float pitch )
|
||||
{
|
||||
this.source = source;
|
||||
this.pos = pos;
|
||||
sound = event;
|
||||
this.volume = volume;
|
||||
this.pitch = pitch;
|
||||
}
|
||||
|
||||
public SpeakerPlayClientMessage( PacketBuffer buf )
|
||||
{
|
||||
source = buf.readUUID();
|
||||
pos = new Vector3d( buf.readDouble(), buf.readDouble(), buf.readDouble() );
|
||||
sound = buf.readResourceLocation();
|
||||
volume = buf.readFloat();
|
||||
pitch = buf.readFloat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
buf.writeUUID( source );
|
||||
buf.writeDouble( pos.x() );
|
||||
buf.writeDouble( pos.y() );
|
||||
buf.writeDouble( pos.z() );
|
||||
buf.writeResourceLocation( sound );
|
||||
buf.writeFloat( volume );
|
||||
buf.writeFloat( pitch );
|
||||
}
|
||||
|
||||
@Override
|
||||
@OnlyIn( Dist.CLIENT )
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
SoundEvent sound = ForgeRegistries.SOUND_EVENTS.getValue( this.sound );
|
||||
if( sound != null ) SoundManager.playSound( source, pos, sound, volume, pitch );
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.shared.network.client;
|
||||
|
||||
import dan200.computercraft.client.SoundManager;
|
||||
import dan200.computercraft.shared.network.NetworkMessage;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Stops a sound on the client
|
||||
*
|
||||
* Called when a speaker is broken.
|
||||
*
|
||||
* @see dan200.computercraft.shared.peripheral.speaker.TileSpeaker
|
||||
*/
|
||||
public class SpeakerStopClientMessage implements NetworkMessage
|
||||
{
|
||||
private final UUID source;
|
||||
|
||||
public SpeakerStopClientMessage( UUID source )
|
||||
{
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public SpeakerStopClientMessage( PacketBuffer buf )
|
||||
{
|
||||
source = buf.readUUID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
buf.writeUUID( source );
|
||||
}
|
||||
|
||||
@Override
|
||||
@OnlyIn( Dist.CLIENT )
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
SoundManager.stopSound( source );
|
||||
}
|
||||
}
|
@@ -54,36 +54,36 @@ public class TerminalState
|
||||
|
||||
if( terminal == null )
|
||||
{
|
||||
this.width = this.height = 0;
|
||||
this.buffer = null;
|
||||
width = height = 0;
|
||||
buffer = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.width = terminal.getWidth();
|
||||
this.height = terminal.getHeight();
|
||||
width = terminal.getWidth();
|
||||
height = terminal.getHeight();
|
||||
|
||||
ByteBuf buf = this.buffer = Unpooled.buffer();
|
||||
ByteBuf buf = buffer = Unpooled.buffer();
|
||||
terminal.write( new PacketBuffer( buf ) );
|
||||
}
|
||||
}
|
||||
|
||||
public TerminalState( PacketBuffer buf )
|
||||
{
|
||||
this.colour = buf.readBoolean();
|
||||
this.compress = buf.readBoolean();
|
||||
colour = buf.readBoolean();
|
||||
compress = buf.readBoolean();
|
||||
|
||||
if( buf.readBoolean() )
|
||||
{
|
||||
this.width = buf.readVarInt();
|
||||
this.height = buf.readVarInt();
|
||||
width = buf.readVarInt();
|
||||
height = buf.readVarInt();
|
||||
|
||||
int length = buf.readVarInt();
|
||||
this.buffer = readCompressed( buf, length, compress );
|
||||
buffer = readCompressed( buf, length, compress );
|
||||
}
|
||||
else
|
||||
{
|
||||
this.width = this.height = 0;
|
||||
this.buffer = null;
|
||||
width = height = 0;
|
||||
buffer = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.shared.network.client;
|
||||
|
||||
import dan200.computercraft.client.gui.ComputerScreenBase;
|
||||
import dan200.computercraft.client.gui.OptionScreen;
|
||||
import dan200.computercraft.shared.computer.upload.UploadResult;
|
||||
import dan200.computercraft.shared.network.NetworkMessage;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class UploadResultMessage implements NetworkMessage
|
||||
{
|
||||
public static final UploadResultMessage COMPUTER_OFF = new UploadResultMessage( UploadResult.ERROR, UploadResult.COMPUTER_OFF_MSG );
|
||||
public static final UploadResultMessage OUT_OF_SPACE = new UploadResultMessage( UploadResult.ERROR, UploadResult.OUT_OF_SPACE_MSG );
|
||||
|
||||
private final UploadResult result;
|
||||
private final ITextComponent message;
|
||||
|
||||
public UploadResultMessage( UploadResult result, ITextComponent message )
|
||||
{
|
||||
this.result = result;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public UploadResultMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
result = buf.readEnum( UploadResult.class );
|
||||
message = buf.readComponent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
buf.writeEnum( result );
|
||||
buf.writeComponent( message );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
Minecraft minecraft = Minecraft.getInstance();
|
||||
|
||||
Screen screen = OptionScreen.unwrap( minecraft.screen );
|
||||
if( screen instanceof ComputerScreenBase<?> )
|
||||
{
|
||||
((ComputerScreenBase<?>) screen).uploadResult( result, message );
|
||||
}
|
||||
}
|
||||
}
|
@@ -16,14 +16,14 @@ public class ComputerContainerData implements ContainerData
|
||||
|
||||
public ComputerContainerData( ServerComputer computer )
|
||||
{
|
||||
this.id = computer.getInstanceID();
|
||||
this.family = computer.getFamily();
|
||||
id = computer.getInstanceID();
|
||||
family = computer.getFamily();
|
||||
}
|
||||
|
||||
public ComputerContainerData( PacketBuffer buf )
|
||||
{
|
||||
this.id = buf.readInt();
|
||||
this.family = buf.readEnum( ComputerFamily.class );
|
||||
id = buf.readInt();
|
||||
family = buf.readEnum( ComputerFamily.class );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -8,12 +8,13 @@ package dan200.computercraft.shared.network.server;
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class ComputerActionServerMessage extends ComputerServerMessage
|
||||
{
|
||||
private Action action;
|
||||
private final Action action;
|
||||
|
||||
public ComputerActionServerMessage( int instanceId, Action action )
|
||||
{
|
||||
@@ -21,8 +22,10 @@ public class ComputerActionServerMessage extends ComputerServerMessage
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public ComputerActionServerMessage()
|
||||
public ComputerActionServerMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
action = buf.readEnum( Action.class );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,14 +36,7 @@ public class ComputerActionServerMessage extends ComputerServerMessage
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
action = buf.readEnum( Action.class );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
protected void handle( NetworkEvent.Context context, @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
switch( action )
|
||||
{
|
||||
|
@@ -22,15 +22,16 @@ import javax.annotation.Nonnull;
|
||||
*/
|
||||
public abstract class ComputerServerMessage implements NetworkMessage
|
||||
{
|
||||
private int instanceId;
|
||||
private final int instanceId;
|
||||
|
||||
public ComputerServerMessage( int instanceId )
|
||||
{
|
||||
this.instanceId = instanceId;
|
||||
}
|
||||
|
||||
public ComputerServerMessage()
|
||||
public ComputerServerMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
instanceId = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -39,12 +40,6 @@ public abstract class ComputerServerMessage implements NetworkMessage
|
||||
buf.writeVarInt( instanceId );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
instanceId = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
@@ -54,8 +49,8 @@ public abstract class ComputerServerMessage implements NetworkMessage
|
||||
IContainerComputer container = computer.getContainer( context.getSender() );
|
||||
if( container == null ) return;
|
||||
|
||||
handle( computer, container );
|
||||
handle( context, computer, container );
|
||||
}
|
||||
|
||||
protected abstract void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container );
|
||||
protected abstract void handle( NetworkEvent.Context context, @Nonnull ServerComputer computer, @Nonnull IContainerComputer container );
|
||||
}
|
||||
|
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.shared.network.server;
|
||||
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import net.minecraft.entity.player.ServerPlayerEntity;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class ContinueUploadMessage extends ComputerServerMessage
|
||||
{
|
||||
private final boolean overwrite;
|
||||
|
||||
public ContinueUploadMessage( int instanceId, boolean overwrite )
|
||||
{
|
||||
super( instanceId );
|
||||
this.overwrite = overwrite;
|
||||
}
|
||||
|
||||
public ContinueUploadMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
overwrite = buf.readBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.toBytes( buf );
|
||||
buf.writeBoolean( overwrite );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( NetworkEvent.Context context, @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
ServerPlayerEntity player = context.getSender();
|
||||
if( player != null ) container.continueUpload( player, overwrite );
|
||||
}
|
||||
}
|
@@ -9,6 +9,7 @@ import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.InputState;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@@ -18,8 +19,8 @@ public class KeyEventServerMessage extends ComputerServerMessage
|
||||
public static final int TYPE_REPEAT = 1;
|
||||
public static final int TYPE_UP = 2;
|
||||
|
||||
private int type;
|
||||
private int key;
|
||||
private final int type;
|
||||
private final int key;
|
||||
|
||||
public KeyEventServerMessage( int instanceId, int type, int key )
|
||||
{
|
||||
@@ -28,8 +29,11 @@ public class KeyEventServerMessage extends ComputerServerMessage
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public KeyEventServerMessage()
|
||||
public KeyEventServerMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
type = buf.readByte();
|
||||
key = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -41,15 +45,7 @@ public class KeyEventServerMessage extends ComputerServerMessage
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
type = buf.readByte();
|
||||
key = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
protected void handle( NetworkEvent.Context context, @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
InputState input = container.getInput();
|
||||
if( type == TYPE_UP )
|
||||
|
@@ -9,6 +9,7 @@ import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.InputState;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@@ -19,10 +20,10 @@ public class MouseEventServerMessage extends ComputerServerMessage
|
||||
public static final int TYPE_UP = 2;
|
||||
public static final int TYPE_SCROLL = 3;
|
||||
|
||||
private int type;
|
||||
private int x;
|
||||
private int y;
|
||||
private int arg;
|
||||
private final int type;
|
||||
private final int x;
|
||||
private final int y;
|
||||
private final int arg;
|
||||
|
||||
public MouseEventServerMessage( int instanceId, int type, int arg, int x, int y )
|
||||
{
|
||||
@@ -33,8 +34,13 @@ public class MouseEventServerMessage extends ComputerServerMessage
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public MouseEventServerMessage()
|
||||
public MouseEventServerMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
type = buf.readByte();
|
||||
arg = buf.readVarInt();
|
||||
x = buf.readVarInt();
|
||||
y = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -48,17 +54,7 @@ public class MouseEventServerMessage extends ComputerServerMessage
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
type = buf.readByte();
|
||||
arg = buf.readVarInt();
|
||||
x = buf.readVarInt();
|
||||
y = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
protected void handle( NetworkEvent.Context context, @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
InputState input = container.getInput();
|
||||
switch( type )
|
||||
|
@@ -10,6 +10,7 @@ import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import dan200.computercraft.shared.util.NBTUtil;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
@@ -22,8 +23,8 @@ import javax.annotation.Nullable;
|
||||
*/
|
||||
public class QueueEventServerMessage extends ComputerServerMessage
|
||||
{
|
||||
private String event;
|
||||
private Object[] args;
|
||||
private final String event;
|
||||
private final Object[] args;
|
||||
|
||||
public QueueEventServerMessage( int instanceId, @Nonnull String event, @Nullable Object[] args )
|
||||
{
|
||||
@@ -32,8 +33,13 @@ public class QueueEventServerMessage extends ComputerServerMessage
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
public QueueEventServerMessage()
|
||||
public QueueEventServerMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
event = buf.readUtf( Short.MAX_VALUE );
|
||||
|
||||
CompoundNBT args = buf.readNbt();
|
||||
this.args = args == null ? null : NBTUtil.decodeObjects( args );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -45,17 +51,7 @@ public class QueueEventServerMessage extends ComputerServerMessage
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
event = buf.readUtf( Short.MAX_VALUE );
|
||||
|
||||
CompoundNBT args = buf.readNbt();
|
||||
this.args = args == null ? null : NBTUtil.decodeObjects( args );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
protected void handle( NetworkEvent.Context context, @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
computer.queueEvent( event, args );
|
||||
}
|
||||
|
@@ -15,15 +15,16 @@ import javax.annotation.Nonnull;
|
||||
|
||||
public class RequestComputerMessage implements NetworkMessage
|
||||
{
|
||||
private int instance;
|
||||
private final int instance;
|
||||
|
||||
public RequestComputerMessage( int instance )
|
||||
{
|
||||
this.instance = instance;
|
||||
}
|
||||
|
||||
public RequestComputerMessage()
|
||||
public RequestComputerMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
instance = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,12 +33,6 @@ public class RequestComputerMessage implements NetworkMessage
|
||||
buf.writeVarInt( instance );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
instance = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
|
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.shared.network.server;
|
||||
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import dan200.computercraft.shared.computer.upload.FileUpload;
|
||||
import net.minecraft.entity.player.ServerPlayerEntity;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class UploadFileMessage extends ComputerServerMessage
|
||||
{
|
||||
public static final int MAX_SIZE = 30 * 1024; // Max packet size is 32767. TODO: Bump this in the future
|
||||
private final List<FileUpload> files;
|
||||
|
||||
public UploadFileMessage( int instanceId, List<FileUpload> files )
|
||||
{
|
||||
super( instanceId );
|
||||
this.files = files;
|
||||
}
|
||||
|
||||
public UploadFileMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
int nFiles = buf.readVarInt();
|
||||
List<FileUpload> files = this.files = new ArrayList<>( nFiles );
|
||||
for( int i = 0; i < nFiles; i++ )
|
||||
{
|
||||
String name = buf.readUtf( 32767 );
|
||||
int size = buf.readVarInt();
|
||||
if( size > MAX_SIZE ) break;
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocateDirect( size );
|
||||
buf.readBytes( buffer );
|
||||
buffer.flip();
|
||||
|
||||
files.add( new FileUpload( name, buffer ) );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.toBytes( buf );
|
||||
buf.writeVarInt( files.size() );
|
||||
for( FileUpload file : files )
|
||||
{
|
||||
buf.writeUtf( file.getName() );
|
||||
buf.writeVarInt( file.getBytes().remaining() );
|
||||
buf.writeBytes( file.getBytes() );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( NetworkEvent.Context context, @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
ServerPlayerEntity player = context.getSender();
|
||||
if( player != null ) container.upload( player, files );
|
||||
}
|
||||
}
|
@@ -24,7 +24,7 @@ final class SaturatedMethod
|
||||
SaturatedMethod( Object target, NamedMethod<PeripheralMethod> method )
|
||||
{
|
||||
this.target = target;
|
||||
this.name = method.getName();
|
||||
name = method.getName();
|
||||
this.method = method.getMethod();
|
||||
}
|
||||
|
||||
|
@@ -11,7 +11,7 @@ import dan200.computercraft.api.network.wired.IWiredElement;
|
||||
import dan200.computercraft.api.network.wired.IWiredNode;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.shared.Registry;
|
||||
import dan200.computercraft.shared.command.CommandCopy;
|
||||
import dan200.computercraft.shared.command.text.ChatHelpers;
|
||||
import dan200.computercraft.shared.common.TileGeneric;
|
||||
import dan200.computercraft.shared.peripheral.modem.ModemState;
|
||||
import dan200.computercraft.shared.util.CapabilityUtil;
|
||||
@@ -53,7 +53,7 @@ public class TileCable extends TileGeneric
|
||||
@Override
|
||||
public World getWorld()
|
||||
{
|
||||
return TileCable.this.getLevel();
|
||||
return getLevel();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -268,12 +268,12 @@ public class TileCable extends TileGeneric
|
||||
if( oldName != null )
|
||||
{
|
||||
player.displayClientMessage( new TranslationTextComponent( "chat.computercraft.wired_modem.peripheral_disconnected",
|
||||
CommandCopy.createCopyText( oldName ) ), false );
|
||||
ChatHelpers.copy( oldName ) ), false );
|
||||
}
|
||||
if( newName != null )
|
||||
{
|
||||
player.displayClientMessage( new TranslationTextComponent( "chat.computercraft.wired_modem.peripheral_connected",
|
||||
CommandCopy.createCopyText( newName ) ), false );
|
||||
ChatHelpers.copy( newName ) ), false );
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -10,7 +10,7 @@ import dan200.computercraft.api.ComputerCraftAPI;
|
||||
import dan200.computercraft.api.network.wired.IWiredElement;
|
||||
import dan200.computercraft.api.network.wired.IWiredNode;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.shared.command.CommandCopy;
|
||||
import dan200.computercraft.shared.command.text.ChatHelpers;
|
||||
import dan200.computercraft.shared.common.TileGeneric;
|
||||
import dan200.computercraft.shared.peripheral.modem.ModemState;
|
||||
import dan200.computercraft.shared.util.CapabilityUtil;
|
||||
@@ -218,7 +218,7 @@ public class TileWiredModemFull extends TileGeneric
|
||||
for( int i = 0; i < names.size(); i++ )
|
||||
{
|
||||
if( i > 0 ) base.append( ", " );
|
||||
base.append( CommandCopy.createCopyText( names.get( i ) ) );
|
||||
base.append( ChatHelpers.copy( names.get( i ) ) );
|
||||
}
|
||||
|
||||
player.displayClientMessage( new TranslationTextComponent( kind, base ), false );
|
||||
|
@@ -87,7 +87,7 @@ public class BlockMonitor extends BlockGeneric
|
||||
{
|
||||
TileMonitor monitor = (TileMonitor) entity;
|
||||
// Defer the block update if we're being placed by another TE. See #691
|
||||
if ( livingEntity == null || livingEntity instanceof FakePlayer )
|
||||
if( livingEntity == null || livingEntity instanceof FakePlayer )
|
||||
{
|
||||
monitor.updateNeighborsDeferred();
|
||||
return;
|
||||
|
@@ -149,7 +149,7 @@ public class TileMonitor extends TileGeneric
|
||||
@Override
|
||||
public void blockTick()
|
||||
{
|
||||
if ( needsUpdate )
|
||||
if( needsUpdate )
|
||||
{
|
||||
needsUpdate = false;
|
||||
updateNeighbors();
|
||||
@@ -279,8 +279,6 @@ public class TileMonitor extends TileGeneric
|
||||
|
||||
int oldXIndex = xIndex;
|
||||
int oldYIndex = yIndex;
|
||||
int oldWidth = width;
|
||||
int oldHeight = height;
|
||||
|
||||
xIndex = nbt.getInt( NBT_X );
|
||||
yIndex = nbt.getInt( NBT_Y );
|
||||
@@ -300,13 +298,6 @@ public class TileMonitor extends TileGeneric
|
||||
// If we're the origin terminal then create it.
|
||||
if( clientMonitor == null ) clientMonitor = new ClientMonitor( advanced, this );
|
||||
}
|
||||
|
||||
if( oldXIndex != xIndex || oldYIndex != yIndex ||
|
||||
oldWidth != width || oldHeight != height )
|
||||
{
|
||||
// One of our properties has changed, so ensure we redraw the block
|
||||
updateBlock();
|
||||
}
|
||||
}
|
||||
|
||||
public final void read( TerminalState state )
|
||||
|
@@ -10,17 +10,23 @@ import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
import dan200.computercraft.api.lua.LuaFunction;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.shared.network.NetworkHandler;
|
||||
import dan200.computercraft.shared.network.client.SpeakerMoveClientMessage;
|
||||
import dan200.computercraft.shared.network.client.SpeakerPlayClientMessage;
|
||||
import net.minecraft.network.play.server.SPlaySoundPacket;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.state.properties.NoteBlockInstrument;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.ResourceLocationException;
|
||||
import net.minecraft.util.SoundCategory;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import net.minecraft.util.math.vector.Vector3d;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static dan200.computercraft.api.lua.LuaValues.checkFinite;
|
||||
@@ -32,20 +38,44 @@ import static dan200.computercraft.api.lua.LuaValues.checkFinite;
|
||||
*/
|
||||
public abstract class SpeakerPeripheral implements IPeripheral
|
||||
{
|
||||
private static final int MIN_TICKS_BETWEEN_SOUNDS = 1;
|
||||
|
||||
private long clock = 0;
|
||||
private long lastPlayTime = 0;
|
||||
private final AtomicInteger notesThisTick = new AtomicInteger();
|
||||
|
||||
private long lastPositionTime;
|
||||
private Vector3d lastPosition;
|
||||
|
||||
public void update()
|
||||
{
|
||||
clock++;
|
||||
notesThisTick.set( 0 );
|
||||
|
||||
// Push position updates to any speakers which have ever played a note,
|
||||
// have moved by a non-trivial amount and haven't had a position update
|
||||
// in the last second.
|
||||
if( lastPlayTime > 0 && (clock - lastPositionTime) >= 20 )
|
||||
{
|
||||
Vector3d position = getPosition();
|
||||
if( lastPosition == null || lastPosition.distanceToSqr( position ) >= 0.1 )
|
||||
{
|
||||
lastPosition = position;
|
||||
lastPositionTime = clock;
|
||||
NetworkHandler.sendToAllTracking(
|
||||
new SpeakerMoveClientMessage( getSource(), position ),
|
||||
getWorld().getChunkAt( new BlockPos( position ) )
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract World getWorld();
|
||||
|
||||
public abstract Vector3d getPosition();
|
||||
|
||||
protected abstract UUID getSource();
|
||||
|
||||
public boolean madeSound( long ticks )
|
||||
{
|
||||
return clock - lastPlayTime <= ticks;
|
||||
@@ -135,26 +165,37 @@ public abstract class SpeakerPeripheral implements IPeripheral
|
||||
|
||||
private synchronized boolean playSound( ILuaContext context, ResourceLocation name, float volume, float pitch, boolean isNote ) throws LuaException
|
||||
{
|
||||
if( clock - lastPlayTime < TileSpeaker.MIN_TICKS_BETWEEN_SOUNDS &&
|
||||
(!isNote || clock - lastPlayTime != 0 || notesThisTick.get() >= ComputerCraft.maxNotesPerTick) )
|
||||
if( clock - lastPlayTime < MIN_TICKS_BETWEEN_SOUNDS )
|
||||
{
|
||||
// Rate limiting occurs when we've already played a sound within the last tick, or we've
|
||||
// played more notes than allowable within the current tick.
|
||||
return false;
|
||||
// Rate limiting occurs when we've already played a sound within the last tick.
|
||||
if( !isNote ) return false;
|
||||
// Or we've played more notes than allowable within the current tick.
|
||||
if( clock - lastPlayTime != 0 || notesThisTick.get() >= ComputerCraft.maxNotesPerTick ) return false;
|
||||
}
|
||||
|
||||
World world = getWorld();
|
||||
Vector3d pos = getPosition();
|
||||
|
||||
float range = MathHelper.clamp( volume, 1.0f, 3.0f ) * 16;
|
||||
|
||||
context.issueMainThreadTask( () -> {
|
||||
MinecraftServer server = world.getServer();
|
||||
if( server == null ) return null;
|
||||
|
||||
float adjVolume = Math.min( volume, 3.0f );
|
||||
server.getPlayerList().broadcast(
|
||||
null, pos.x, pos.y, pos.z, adjVolume > 1.0f ? 16 * adjVolume : 16.0, world.dimension(),
|
||||
new SPlaySoundPacket( name, SoundCategory.RECORDS, pos, adjVolume, pitch )
|
||||
);
|
||||
if( isNote )
|
||||
{
|
||||
server.getPlayerList().broadcast(
|
||||
null, pos.x, pos.y, pos.z, range, world.dimension(),
|
||||
new SPlaySoundPacket( name, SoundCategory.RECORDS, pos, range, pitch )
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
NetworkHandler.sendToAllAround(
|
||||
new SpeakerPlayClientMessage( getSource(), pos, name, range, pitch ),
|
||||
world, pos, range
|
||||
);
|
||||
}
|
||||
return null;
|
||||
} );
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user