Skip to content

Commit

Permalink
Replace a lot of Optional.of with ofNullable
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Jun 1, 2024
1 parent a4ac352 commit 32b772c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ loader_version=0.15.11
fabric_kotlin_version=1.10.20+kotlin.1.9.24

# Mod Properties
mod_version=1.6.0
mod_version=1.6.1
maven_group=dev.andante
25 changes: 25 additions & 0 deletions src/main/java/dev/andante/codex/mixin/DataResultErrorMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.andante.codex.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.Optional;

@Mixin(targets = { "com.mojang.serialization.DataResult$Error" })
public class DataResultErrorMixin<R> {
@Redirect(method = "error", at = @At(value = "INVOKE", target = "Ljava/util/Optional;of(Ljava/lang/Object;)Ljava/util/Optional;"), remap = false)
private Optional<R> onErrorOf(R value) {
return Optional.ofNullable(value);
}

@Redirect(method = "flatMap(Ljava/util/function/Function;)Lcom/mojang/serialization/DataResult$Error;", at = @At(value = "INVOKE", target = "Ljava/util/Optional;of(Ljava/lang/Object;)Ljava/util/Optional;"), remap = false)
private Optional<R> onFlatMapOf(R value) {
return Optional.ofNullable(value);
}

@Redirect(method = "setPartial(Ljava/lang/Object;)Lcom/mojang/serialization/DataResult$Error;", at = @At(value = "INVOKE", target = "Ljava/util/Optional;of(Ljava/lang/Object;)Ljava/util/Optional;"), remap = false)
private Optional<R> onSetPartialOf(R value) {
return Optional.ofNullable(value);
}
}
25 changes: 25 additions & 0 deletions src/main/java/dev/andante/codex/mixin/DataResultSuccessMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.andante.codex.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.Optional;

@Mixin(targets = { "com.mojang.serialization.DataResult$Success" })
public class DataResultSuccessMixin<R> {
@Redirect(method = "result", at = @At(value = "INVOKE", target = "Ljava/util/Optional;of(Ljava/lang/Object;)Ljava/util/Optional;"), remap = false)
private Optional<R> onResultOf(R value) {
return Optional.ofNullable(value);
}

@Redirect(method = "resultOrPartial()Ljava/util/Optional;", at = @At(value = "INVOKE", target = "Ljava/util/Optional;of(Ljava/lang/Object;)Ljava/util/Optional;"), remap = false)
private Optional<R> onResultOrPartialOf(R value) {
return Optional.ofNullable(value);
}

@Redirect(method = "resultOrPartial(Ljava/util/function/Consumer;)Ljava/util/Optional;", at = @At(value = "INVOKE", target = "Ljava/util/Optional;of(Ljava/lang/Object;)Ljava/util/Optional;"), remap = false)
private Optional<R> onResultOrPartialConsumerOf(R value) {
return Optional.ofNullable(value);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/codex.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"package": "dev.andante.codex.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"DataResultErrorMixin",
"DataResultSuccessMixin",
"EitherLeftMixin",
"EitherRightMixin"
],
Expand Down

0 comments on commit 32b772c

Please sign in to comment.