update OreAddon to use do notation

This commit is contained in:
dfsek
2022-08-15 11:12:10 -07:00
parent 8538ee6804
commit 45c3729392
5 changed files with 67 additions and 15 deletions

View File

@@ -8,25 +8,27 @@
package com.dfsek.terra.addons.ore;
import com.dfsek.terra.addons.manifest.api.MonadAddonInitializer;
import com.dfsek.terra.addons.manifest.api.monad.Do;
import com.dfsek.terra.addons.manifest.api.monad.Get;
import com.dfsek.terra.addons.manifest.api.monad.Init;
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
import com.dfsek.terra.api.util.function.monad.Monad;
public class OreAddon implements MonadAddonInitializer {
@Override
public Init<?> initialize() {
return Get.eventManager()
.map(eventManager -> eventManager.getHandler(FunctionalEventHandler.class))
.bind(functionalEventHandler ->
Get.addon()
.map(addon -> functionalEventHandler
.register(addon, ConfigPackPreLoadEvent.class)
.then(event -> event
.getPack()
.registerConfigType(new OreConfigType(), addon.key("ORE"), 1))
.failThrough()
));
public Monad<?, Init<?>> initialize() {
return Do.with(
Get.eventManager().map(manager -> manager.getHandler(FunctionalEventHandler.class)),
Get.addon(),
((eventHandler, addon) -> Init
.ofPure(eventHandler
.register(addon, ConfigPackPreLoadEvent.class)
.then(event -> event
.getPack()
.registerConfigType(new OreConfigType(), addon.key("ORE"), 1))
.failThrough()))
);
}
}

View File

@@ -1,8 +1,9 @@
package com.dfsek.terra.addons.manifest.api;
import com.dfsek.terra.addons.manifest.api.monad.Init;
import com.dfsek.terra.api.util.function.monad.Monad;
public interface MonadAddonInitializer {
Init<?> initialize();
Monad<?, Init<?>> initialize();
}

View File

@@ -32,6 +32,12 @@ public final class Do {
.bind(bind2.apply(t)));
}
public static <T, U, V, M extends Monad<?, M>> Monad<V, M> with(Monad<T, M> monad,
Monad<U, M> monad2,
Function2<T, U, Monad<V, M>> bind2) {
return with(monad, Function1.constant(monad2), bind2);
}
public static <T, U, V, W, M extends Monad<?, M>> Monad<W, M> with(Monad<T, M> monad,
Function1<T, Monad<U, M>> bind,
Function2<T, U, Monad<V, M>> bind2,
@@ -45,6 +51,20 @@ public final class Do {
.apply(u)));
}
public static <T, U, V, W, M extends Monad<?, M>> Monad<W, M> with(Monad<T, M> monad,
Monad<U, M> monad2,
Function2<T, U, Monad<V, M>> bind2,
Function3<T, U, V, Monad<W, M>> bind3) {
return with(monad, Function1.constant(monad2), bind2, bind3);
}
public static <T, U, V, W, M extends Monad<?, M>> Monad<W, M> with(Monad<T, M> monad,
Monad<U, M> monad2,
Monad<V, M> monad3,
Function3<T, U, V, Monad<W, M>> bind3) {
return with(monad, monad2, Function2.constant(monad3), bind3);
}
public static <T, U, V, W, X, M extends Monad<?, M>> Monad<X, M> with(Monad<T, M> monad,
Function1<T, Monad<U, M>> bind,
Function2<T, U, Monad<V, M>> bind2,
@@ -61,4 +81,28 @@ public final class Do {
.apply(v)))
.apply(u)));
}
public static <T, U, V, W, X, M extends Monad<?, M>> Monad<X, M> with(Monad<T, M> monad,
Monad<U, M> monad2,
Function2<T, U, Monad<V, M>> bind2,
Function3<T, U, V, Monad<W, M>> bind3,
Function4<T, U, V, W, Monad<X, M>> bind4) {
return with(monad, Function1.constant(monad2), bind2, bind3, bind4);
}
public static <T, U, V, W, X, M extends Monad<?, M>> Monad<X, M> with(Monad<T, M> monad,
Monad<U, M> monad2,
Monad<V, M> monad3,
Function3<T, U, V, Monad<W, M>> bind3,
Function4<T, U, V, W, Monad<X, M>> bind4) {
return with(monad, monad2, Function2.constant(monad3), bind3, bind4);
}
public static <T, U, V, W, X, M extends Monad<?, M>> Monad<X, M> with(Monad<T, M> monad,
Monad<U, M> monad2,
Monad<V, M> monad3,
Monad<W, M> monad4,
Function4<T, U, V, W, Monad<X, M>> bind4) {
return with(monad, monad2, monad3, Function3.constant(monad4), bind4);
}
}

View File

@@ -34,6 +34,10 @@ public class Init<T> implements Monad<T, Init<?>> {
@Override
public <U> Monad<U, Init<?>> pure(U u) {
return of(Functions.constant(u));
return ofPure(u);
}
public static <T> Init<T> ofPure(T t) {
return of(Functions.constant(t));
}
}

View File

@@ -2,6 +2,7 @@ package com.dfsek.terra.addons.manifest.impl;
import com.dfsek.terra.addons.manifest.api.AddonInitializer;
import com.dfsek.terra.addons.manifest.api.MonadAddonInitializer;
import com.dfsek.terra.addons.manifest.api.monad.Init;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.inject.Injector;
@@ -34,7 +35,7 @@ public abstract class Initializer {
@Override
public void initialize(InitInfo info) {
addon.initialize().apply(info);
((Init<?>) addon.initialize()).apply(info);
}
}