-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from Robustum/tag_update
タグおよびレシピ回りの拡張を追加
- Loading branch information
Showing
39 changed files
with
1,882 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/dev/robustum/core/mixin/codec/IngredientAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dev.robustum.core.mixin.codec; | ||
|
||
import net.minecraft.recipe.Ingredient; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
@Mixin(Ingredient.class) | ||
public interface IngredientAccessor { | ||
@Accessor | ||
Ingredient.Entry[] getEntries(); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/dev/robustum/core/mixin/tag/ClientPlayNetworkHandlerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dev.robustum.core.mixin.tag; | ||
|
||
import dev.robustum.core.tag.RobustumTagEvents; | ||
import net.fabricmc.api.EnvType; | ||
import net.minecraft.client.network.ClientPlayNetworkHandler; | ||
import net.minecraft.network.packet.s2c.play.SynchronizeTagsS2CPacket; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
@Mixin(ClientPlayNetworkHandler.class) | ||
public abstract class ClientPlayNetworkHandlerMixin { | ||
@Unique | ||
private static final Logger LOGGER = LogManager.getLogger(ClientPlayNetworkHandlerMixin.class); | ||
|
||
@Inject(method = "onSynchronizeTags", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/MinecraftClient;getSearchableContainer(Lnet/minecraft/client/search/SearchManager$Key;)Lnet/minecraft/client/search/SearchableContainer;")) | ||
private void robustum$onSynchronizeTags(SynchronizeTagsS2CPacket packet, CallbackInfo ci) { | ||
RobustumTagEvents.RELOAD.invoker().onReload(packet.getTagManager(), EnvType.CLIENT); | ||
LOGGER.info("Client TagManager reloaded!"); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/dev/robustum/core/mixin/tag/ServerTagManagerHolderMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dev.robustum.core.mixin.tag; | ||
|
||
import dev.robustum.core.tag.RobustumTagEvents; | ||
import net.fabricmc.api.EnvType; | ||
import net.minecraft.tag.ServerTagManagerHolder; | ||
import net.minecraft.tag.TagManager; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
@Mixin(ServerTagManagerHolder.class) | ||
public abstract class ServerTagManagerHolderMixin { | ||
@Unique | ||
private static final Logger LOGGER = LogManager.getLogger(ServerTagManagerHolderMixin.class); | ||
|
||
@Inject(method = "setTagManager", at = @At("TAIL")) | ||
private static void robustum$setTagManager(TagManager tagManager, CallbackInfo ci) { | ||
RobustumTagEvents.RELOAD.invoker().onReload(tagManager, EnvType.SERVER); | ||
LOGGER.info("Server TagManager reloaded!"); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/dev/robustum/core/mixin/tag/TagGroupLoaderMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package dev.robustum.core.mixin.tag; | ||
|
||
import dev.robustum.core.RobustumCore; | ||
import dev.robustum.core.tag.RobustumTagEvents; | ||
import net.minecraft.resource.ResourceManager; | ||
import net.minecraft.tag.Tag; | ||
import net.minecraft.tag.TagGroupLoader; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.registry.Registry; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.util.Map; | ||
|
||
@Mixin(TagGroupLoader.class) | ||
public abstract class TagGroupLoaderMixin<T> { | ||
@Final | ||
@Shadow | ||
private String entryType; | ||
|
||
@Nullable | ||
@Unique | ||
private Registry<?> findRegistry() { | ||
return switch (entryType) { | ||
case "block" -> Registry.BLOCK; | ||
case "item" -> Registry.ITEM; | ||
case "fluid" -> Registry.FLUID; | ||
case "entity_type" -> Registry.ENTITY_TYPE; | ||
default -> null; | ||
}; | ||
} | ||
|
||
@Inject(method = "method_18243", at = @At("RETURN")) | ||
private void robustum$method_18243(ResourceManager resourceManager, CallbackInfoReturnable<Map<Identifier, Tag.Builder>> cir) { | ||
Registry<?> registry = findRegistry(); | ||
if (registry == null) return; | ||
RobustumTagEvents.REGISTER.invoker().onRegister(new RobustumTagEvents.Helper(registry, (@NotNull Identifier tagId, Tag.@NotNull Entry entry) -> cir.getReturnValue().computeIfAbsent(tagId, k -> Tag.Builder.create()).add(entry, RobustumCore.MOD_NAME))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
package dev.robustum.core | ||
|
||
import dev.robustum.core.recipe.RobustumRecipeSerializers | ||
import net.fabricmc.api.ModInitializer | ||
import net.minecraft.util.Identifier | ||
import org.apache.logging.log4j.LogManager | ||
import org.apache.logging.log4j.Logger | ||
|
||
class RobustumCore : ModInitializer { | ||
companion object { | ||
const val MOD_ID = "robustum_core" | ||
private val logger = LogManager.getLogger(RobustumCore::class.java) | ||
} | ||
object RobustumCore : ModInitializer { | ||
const val MOD_ID = "robustum_core" | ||
const val MOD_NAME = "Robustum Core" | ||
|
||
@JvmStatic | ||
fun id(path: String): Identifier = Identifier(MOD_ID, path) | ||
|
||
private val logger: Logger = LogManager.getLogger(RobustumCore::class.java) | ||
|
||
override fun onInitialize() { | ||
RobustumRecipeSerializers | ||
|
||
logger.info("Robustum Core is loaded!") | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/main/kotlin/dev/robustum/core/codec/KeyDispatchCodec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package dev.robustum.core.codec | ||
|
||
import com.mojang.datafixers.util.Pair | ||
import com.mojang.serialization.Codec | ||
import com.mojang.serialization.DataResult | ||
import com.mojang.serialization.DynamicOps | ||
import com.mojang.serialization.MapCodec | ||
import com.mojang.serialization.MapDecoder | ||
import com.mojang.serialization.MapEncoder | ||
import com.mojang.serialization.MapLike | ||
import com.mojang.serialization.RecordBuilder | ||
import dev.robustum.core.extensions.isErrored | ||
import java.util.function.Function | ||
import java.util.stream.Stream | ||
|
||
class KeyDispatchCodec<K : Any, V : Any>( | ||
val typeKey: String, | ||
val keyCodec: Codec<K>, | ||
val type: (V) -> DataResult<K>, | ||
val decoder: (K) -> DataResult<out MapDecoder<V>>, | ||
val encoder: (V) -> DataResult<out MapEncoder<V>>, | ||
) : MapCodec<V>() { | ||
companion object { | ||
@JvmStatic | ||
private fun <K : Any, V : Any> getCodec( | ||
type: (V) -> DataResult<K>, | ||
codec: (K) -> DataResult<MapCodec<V>>, | ||
input: V, | ||
): DataResult<MapEncoder<V>> = type(input) | ||
.flatMap { key: K -> codec(key).map(Function.identity()) } | ||
} | ||
|
||
constructor( | ||
typeKey: String, | ||
keyCodec: Codec<K>, | ||
type: (V) -> DataResult<K>, | ||
codec: (K) -> DataResult<MapCodec<V>>, | ||
) : this(typeKey, keyCodec, type, codec, { input: V -> getCodec(type, codec, input) }) | ||
|
||
private val valueKey = "value" | ||
|
||
override fun <T : Any> keys(ops: DynamicOps<T>): Stream<T> = Stream.of(typeKey, valueKey).map(ops::createString) | ||
|
||
override fun <T : Any> decode(ops: DynamicOps<T>, input: MapLike<T>): DataResult<V> { | ||
val elementName: T = | ||
input.get(typeKey) ?: return DataResult.error("Input does not contain a key [$typeKey]: $input") | ||
return keyCodec.decode(ops, elementName).flatMap { type1: Pair<K, T> -> | ||
decoder(type1.first).flatMap { elementDecoder: MapDecoder<V> -> | ||
if (ops.compressMaps()) { | ||
input | ||
.get(ops.createString(valueKey)) | ||
?.let { value: T -> elementDecoder.decoder().parse(ops, value).map(Function.identity()) } | ||
?: return@flatMap DataResult.error("Input does not have a \"value\" entry: $input") | ||
} else { | ||
elementDecoder.decode(ops, input).map(Function.identity()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun <T : Any> encode(input: V, ops: DynamicOps<T>, prefix: RecordBuilder<T>): RecordBuilder<T> { | ||
val encodeResult: DataResult<out MapEncoder<V>> = encoder(input) | ||
val builder: RecordBuilder<T> = prefix.withErrorsFrom(encodeResult) | ||
if (encodeResult.isErrored) { | ||
Result | ||
return builder | ||
} | ||
val elementEncoder: MapEncoder<V> = encodeResult.result().get() | ||
return when { | ||
ops.compressMaps() -> | ||
prefix | ||
.add(typeKey, type(input).flatMap { type1: K -> keyCodec.encodeStart(ops, type1) }) | ||
.add(valueKey, elementEncoder.encoder().encodeStart(ops, input)) | ||
|
||
else -> | ||
elementEncoder | ||
.encode(input, ops, prefix) | ||
.add(typeKey, type(input).flatMap { type1: K -> keyCodec.encodeStart(ops, type1) }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dev.robustum.core.codec | ||
|
||
import com.mojang.datafixers.util.Pair | ||
import com.mojang.serialization.Codec | ||
import com.mojang.serialization.DataResult | ||
import com.mojang.serialization.DynamicOps | ||
import com.mojang.serialization.MapLike | ||
import java.util.* | ||
|
||
class OptionalCodec<A : Any>(val codec: Codec<A>) : Codec<Optional<A>> { | ||
override fun <T : Any> encode(input: Optional<A>, ops: DynamicOps<T>, prefix: T): DataResult<T> = when (input.isEmpty) { | ||
true -> DataResult.success(ops.emptyMap()) | ||
false -> codec.encode(input.get(), ops, prefix) | ||
} | ||
|
||
private fun <T : Any> isEmpty(ops: DynamicOps<T>, input: T): Boolean = ops | ||
.getMap(input) | ||
.result() | ||
.map { mapLike: MapLike<T> -> mapLike.entries().findAny().isEmpty } | ||
.orElse(false) | ||
|
||
override fun <T : Any> decode(ops: DynamicOps<T>, input: T): DataResult<Pair<Optional<A>, T>> = when { | ||
isEmpty(ops, input) -> DataResult.success(Pair.of(Optional.empty(), input)) | ||
else -> codec.decode(ops, input).map { pair: Pair<A, T> -> pair.mapFirst(Optional<A>::of) } | ||
} | ||
} |
Oops, something went wrong.