fix(java): fixed broken config and textchange conversions

This commit is contained in:
zaaarf 2024-09-18 01:54:20 +02:00
parent c5ab72280a
commit 7c92b94a07
No known key found for this signature in database
GPG key ID: C91CFF9E2262BBA1
5 changed files with 62 additions and 19 deletions

View file

@ -57,3 +57,47 @@ tasks.register('cargoBuild', Exec) {
processResources.dependsOn cargoBuild
build.finalizedBy shadowJar
publishing {
publications {
mavenJava(MavenPublication) {
artifactId = 'my-library'
from components.java
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
pom {
name = 'My Library'
description = 'A concise description of my library'
url = 'http://www.example.com/library'
properties = [
myProp: "value",
"prop.with.dots": "anotherValue"
]
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'johnd'
name = 'John Doe'
email = 'john.doe@example.com'
}
}
scm {
connection = 'scm:git:git://example.com/my-library.git'
developerConnection = 'scm:git:ssh://example.com/my-library.git'
url = 'http://example.com/my-library/'
}
}
}
}
}

View file

@ -155,4 +155,3 @@ public final class Client {
Extensions.loadLibraryIfNotPresent();
}
}

View file

@ -47,7 +47,7 @@ public class Config {
* @param username the username
* @param password the password
* @param host the host server
* @param port the port CodeMP is running on, must be between 0 and 65535
* @param port the port CodeMP is running on, must be between 0 and 65535 (will be clamped)
* @param tls whether to use TLS
*/
public Config(String username, String password, String host, int port, boolean tls) {
@ -55,14 +55,8 @@ public class Config {
username,
password,
Optional.of(host),
OptionalInt.of(checkPort(port)),
OptionalInt.of(port),
Optional.of(tls)
);
}
private static int checkPort(int port) {
if(port < 0 || port > 65535)
throw new IllegalArgumentException("Port value must be between 0 and 65535!");
return port;
}
}

View file

@ -19,13 +19,13 @@ public class TextChange {
* The starting position of the change.
* If negative, it is clamped to 0.
*/
public final int start;
public final long start;
/**
* The endomg position of the change.
* If negative, it is clamped to 0.
*/
public final int end;
public final long end;
/**
* The content of the change.
@ -74,14 +74,14 @@ public class TextChange {
* @return the mutated string
*/
public String apply(String input) {
int preIndex = Math.min(this.start, input.length());
long preIndex = Math.min(this.start, input.length());
String pre = "";
try {
pre = input.substring(0, preIndex);
pre = input.substring(0, (int) preIndex);
} catch(IndexOutOfBoundsException ignored) {}
String post = "";
try {
post = input.substring(this.end);
post = input.substring((int) this.end);
} catch(IndexOutOfBoundsException ignored) {}
return pre + this.content + post;
}

View file

@ -126,7 +126,7 @@ macro_rules! jobjectify_error {
fn jobjectify($self, env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JObject<'local>, jni::errors::Error> {
let class = env.find_class($jclass)?;
let msg = env.new_string(format!("{:#?}", $self))?;
env.new_object(class, "(Ljava/lang/String)V", &[jni::objects::JValueGen::Object(&msg)])
env.new_object(class, "(Ljava/lang/String;)V", &[jni::objects::JValueGen::Object(&msg)])
}
}
};
@ -332,7 +332,7 @@ impl<'local> Deobjectify<'local, Self> for crate::api::Config {
let port = {
let jfield = env.get_field(&config, "port", "Ljava/util/OptionalInt;")?.l()?;
if env.call_method(&jfield, "isPresent", "()Z", &[])?.z()? {
let ivalue = env.call_method(&jfield, "get", "()I", &[])?.i()?;
let ivalue = env.call_method(&jfield, "getAsInt", "()I", &[])?.i()?;
Some(ivalue.clamp(0, 65535) as u16)
} else {
None
@ -343,7 +343,13 @@ impl<'local> Deobjectify<'local, Self> for crate::api::Config {
let jfield = env.get_field(&config, "host", "Ljava/util/Optional;")?.l()?;
if env.call_method(&jfield, "isPresent", "()Z", &[])?.z()? {
let field = env.call_method(&jfield, "get", "()Ljava/lang/Object;", &[])?.l()?;
Some(env.call_method(field, "booleanValue", "()Z", &[])?.z()?)
let bool_true = env.get_static_field("java/lang/Boolean", "TRUE", "Ljava/lang/Boolean;")?.l()?;
Some(env.call_method(
field,
"equals",
"(Ljava/lang/Object;)Z",
&[jni::objects::JValueGen::Object(&bool_true)]
)?.z()?) // what a joke
} else {
None
}
@ -383,8 +389,8 @@ impl<'local> Deobjectify<'local, Self> for crate::api::Cursor {
impl<'local> Deobjectify<'local, Self> for crate::api::TextChange {
fn deobjectify(env: &mut jni::JNIEnv<'local>, change: jni::objects::JObject<'local>) -> Result<Self, jni::errors::Error> {
let start = env.get_field(&change, "start", "I")?.j()?.max(0) as u32;
let end = env.get_field(&change, "end", "I")?.j()?.max(0) as u32;
let start = env.get_field(&change, "start", "J")?.j()?.clamp(0, u32::MAX.into()) as u32;
let end = env.get_field(&change, "end", "J")?.j()?.clamp(0, u32::MAX.into()) as u32;
let content = {
let jfield = env.get_field(&change, "content", "Ljava/lang/String;")?.l()?;