diff options
author | nea <nea@nea.moe> | 2023-05-16 01:23:43 +0200 |
---|---|---|
committer | nea <nea@nea.moe> | 2023-05-16 01:23:43 +0200 |
commit | ead6762eb1c005914b05f9d3c29f334989c67513 (patch) | |
tree | cd1409756be2bc4a93195c31d432fef053afe002 /src/main/kotlin/moe/nea/firmament/rei | |
parent | 96c546cc73880a7c502c17aadda6ca84c847692d (diff) | |
download | firmament-ead6762eb1c005914b05f9d3c29f334989c67513.tar.gz firmament-ead6762eb1c005914b05f9d3c29f334989c67513.tar.bz2 firmament-ead6762eb1c005914b05f9d3c29f334989c67513.zip |
Replace references to NEU with Firmament
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/rei')
6 files changed, 291 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/rei/FirmamentReiPlugin.kt b/src/main/kotlin/moe/nea/firmament/rei/FirmamentReiPlugin.kt new file mode 100644 index 0000000..992b104 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/rei/FirmamentReiPlugin.kt @@ -0,0 +1,73 @@ +package moe.nea.firmament.rei + +import io.github.moulberry.repo.data.NEUItem +import me.shedaniel.rei.api.client.plugins.REIClientPlugin +import me.shedaniel.rei.api.client.registry.category.CategoryRegistry +import me.shedaniel.rei.api.client.registry.display.DisplayRegistry +import me.shedaniel.rei.api.client.registry.entry.CollapsibleEntryRegistry +import me.shedaniel.rei.api.client.registry.entry.EntryRegistry +import me.shedaniel.rei.api.client.registry.screen.ScreenRegistry +import me.shedaniel.rei.api.common.entry.EntryStack +import me.shedaniel.rei.api.common.entry.type.EntryTypeRegistry +import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes +import net.minecraft.item.ItemStack +import net.minecraft.text.Text +import net.minecraft.util.Identifier +import moe.nea.firmament.recipes.SBCraftingRecipe +import moe.nea.firmament.recipes.SBForgeRecipe +import moe.nea.firmament.repo.ItemCache.asItemStack +import moe.nea.firmament.repo.RepoManager +import moe.nea.firmament.util.SkyblockId + + +class FirmamentReiPlugin : REIClientPlugin { + + companion object { + fun EntryStack<NEUItem>.asItemEntry(): EntryStack<ItemStack> { + return EntryStack.of(VanillaEntryTypes.ITEM, value.asItemStack()) + } + + val SKYBLOCK_ITEM_TYPE_ID = Identifier("firmament", "skyblockitems") + } + + override fun registerEntryTypes(registry: EntryTypeRegistry) { + registry.register(SKYBLOCK_ITEM_TYPE_ID, SBItemEntryDefinition) + } + + override fun registerCategories(registry: CategoryRegistry) { + registry.add(SBCraftingRecipe.Category) + registry.add(SBForgeRecipe.Category) + } + + override fun registerDisplays(registry: DisplayRegistry) { + registry.registerDisplayGenerator( + SBCraftingRecipe.Category.catIdentifier, + SkyblockCraftingRecipeDynamicGenerator + ) + registry.registerDisplayGenerator( + SBForgeRecipe.Category.categoryIdentifier, + SkyblockForgeRecipeDynamicGenerator + ) + } + + override fun registerCollapsibleEntries(registry: CollapsibleEntryRegistry) { + RepoManager.neuRepo.constants.parents.parents + .forEach { (parent, children) -> + registry.group( + SkyblockId(parent).identifier, + Text.literal(RepoManager.getNEUItem(SkyblockId(parent))?.displayName ?: parent), + (children + parent).map { SBItemEntryDefinition.getEntry(RepoManager.getNEUItem(SkyblockId(it))) }) + } + } + + override fun registerScreens(registry: ScreenRegistry) { + registry.registerFocusedStack(SkyblockItemIdFocusedStackProvider) + } + + override fun registerEntries(registry: EntryRegistry) { + RepoManager.neuRepo.items?.items?.values?.forEach { + if (!it.isVanilla) + registry.addEntry(EntryStack.of(SBItemEntryDefinition, it)) + } + } +} diff --git a/src/main/kotlin/moe/nea/firmament/rei/NEUItemEntryRenderer.kt b/src/main/kotlin/moe/nea/firmament/rei/NEUItemEntryRenderer.kt new file mode 100644 index 0000000..8253c96 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/rei/NEUItemEntryRenderer.kt @@ -0,0 +1,31 @@ +package moe.nea.firmament.rei + +import io.github.moulberry.repo.data.NEUItem +import me.shedaniel.math.Rectangle +import me.shedaniel.rei.api.client.entry.renderer.EntryRenderer +import me.shedaniel.rei.api.client.gui.widgets.Tooltip +import me.shedaniel.rei.api.client.gui.widgets.TooltipContext +import me.shedaniel.rei.api.common.entry.EntryStack +import net.minecraft.client.util.math.MatrixStack +import moe.nea.firmament.rei.FirmamentReiPlugin.Companion.asItemEntry + +object NEUItemEntryRenderer : EntryRenderer<NEUItem> { + override fun render( + entry: EntryStack<NEUItem>, + matrices: MatrixStack, + bounds: Rectangle, + mouseX: Int, + mouseY: Int, + delta: Float + ) { + matrices.push() + matrices.translate(0F, 0F, 100F) + entry.asItemEntry().render(matrices, bounds, mouseX, mouseY, delta) + matrices.pop() + } + + override fun getTooltip(entry: EntryStack<NEUItem>, tooltipContext: TooltipContext): Tooltip? { + return entry.asItemEntry().getTooltip(tooltipContext, false) + } + +} diff --git a/src/main/kotlin/moe/nea/firmament/rei/NEUItemEntrySerializer.kt b/src/main/kotlin/moe/nea/firmament/rei/NEUItemEntrySerializer.kt new file mode 100644 index 0000000..15731cb --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/rei/NEUItemEntrySerializer.kt @@ -0,0 +1,25 @@ +package moe.nea.firmament.rei + +import io.github.moulberry.repo.data.NEUItem +import me.shedaniel.rei.api.common.entry.EntrySerializer +import me.shedaniel.rei.api.common.entry.EntryStack +import net.minecraft.nbt.NbtCompound +import moe.nea.firmament.repo.RepoManager +import moe.nea.firmament.util.SkyblockId + +object NEUItemEntrySerializer : EntrySerializer<NEUItem?> { + const val SKYBLOCK_ID_ENTRY = "SKYBLOCK_ID" + + override fun supportSaving(): Boolean = true + override fun supportReading(): Boolean = true + + override fun read(tag: NbtCompound): NEUItem? { + return RepoManager.getNEUItem(SkyblockId(tag.getString(SKYBLOCK_ID_ENTRY))) + } + + override fun save(entry: EntryStack<NEUItem?>, value: NEUItem?): NbtCompound { + return NbtCompound().apply { + putString(SKYBLOCK_ID_ENTRY, value?.skyblockItemId ?: "null") + } + } +} diff --git a/src/main/kotlin/moe/nea/firmament/rei/SBItemEntryDefinition.kt b/src/main/kotlin/moe/nea/firmament/rei/SBItemEntryDefinition.kt new file mode 100644 index 0000000..f8dbce7 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/rei/SBItemEntryDefinition.kt @@ -0,0 +1,85 @@ +package moe.nea.firmament.rei + +import io.github.moulberry.repo.data.NEUIngredient +import io.github.moulberry.repo.data.NEUItem +import java.util.stream.Stream +import me.shedaniel.rei.api.client.entry.renderer.EntryRenderer +import me.shedaniel.rei.api.common.entry.EntrySerializer +import me.shedaniel.rei.api.common.entry.EntryStack +import me.shedaniel.rei.api.common.entry.comparison.ComparisonContext +import me.shedaniel.rei.api.common.entry.type.EntryDefinition +import me.shedaniel.rei.api.common.entry.type.EntryType +import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes +import net.minecraft.item.ItemStack +import net.minecraft.registry.tag.TagKey +import net.minecraft.text.Text +import net.minecraft.util.Identifier +import moe.nea.firmament.rei.FirmamentReiPlugin.Companion.asItemEntry +import moe.nea.firmament.repo.ItemCache.asItemStack +import moe.nea.firmament.repo.ItemCache.getIdentifier +import moe.nea.firmament.repo.RepoManager +import moe.nea.firmament.util.SkyblockId + +// TODO: allow stackable entries +object SBItemEntryDefinition : EntryDefinition<NEUItem> { + override fun equals(o1: NEUItem?, o2: NEUItem?, context: ComparisonContext?): Boolean { + return o1 === o2 + } + + override fun cheatsAs(entry: EntryStack<NEUItem>?, value: NEUItem?): ItemStack { + return value.asItemStack() + } + + override fun getValueType(): Class<NEUItem> = NEUItem::class.java + override fun getType(): EntryType<NEUItem> = EntryType.deferred(FirmamentReiPlugin.SKYBLOCK_ITEM_TYPE_ID) + + override fun getRenderer(): EntryRenderer<NEUItem> = NEUItemEntryRenderer + + override fun getSerializer(): EntrySerializer<NEUItem?> { + return NEUItemEntrySerializer + } + + override fun getTagsFor(entry: EntryStack<NEUItem>?, value: NEUItem?): Stream<out TagKey<*>>? { + return Stream.empty() + } + + override fun asFormattedText(entry: EntryStack<NEUItem>, value: NEUItem): Text { + return VanillaEntryTypes.ITEM.definition.asFormattedText(entry.asItemEntry(), value.asItemStack()) + } + + override fun hash(entry: EntryStack<NEUItem>, value: NEUItem?, context: ComparisonContext): Long { + // Repo items are immutable, and get replaced entirely when loaded from disk + return System.identityHashCode(value) * 31L + } + + override fun wildcard(entry: EntryStack<NEUItem>?, value: NEUItem?): NEUItem? { + return value + } + + override fun normalize(entry: EntryStack<NEUItem>?, value: NEUItem?): NEUItem? { + return value + } + + override fun copy(entry: EntryStack<NEUItem>?, value: NEUItem?): NEUItem? { + return value + } + + override fun isEmpty(entry: EntryStack<NEUItem>?, value: NEUItem?): Boolean { + return false + } + + override fun getIdentifier(entry: EntryStack<NEUItem>?, value: NEUItem?): Identifier { + return value?.getIdentifier() ?: Identifier.of("skyblockitem", "null")!! + } + + fun getEntry(neuItem: NEUItem?): EntryStack<NEUItem> = + EntryStack.of(this, neuItem) + + fun getEntry(skyblockId: SkyblockId?): EntryStack<NEUItem> = + EntryStack.of(this, skyblockId?.let { RepoManager.getNEUItem(it) }) + + fun getEntry(ingredient: NEUIngredient?): EntryStack<NEUItem> = + getEntry(ingredient?.itemId?.let { SkyblockId(it) }) + + +} diff --git a/src/main/kotlin/moe/nea/firmament/rei/SkyblockCraftingRecipeDynamicGenerator.kt b/src/main/kotlin/moe/nea/firmament/rei/SkyblockCraftingRecipeDynamicGenerator.kt new file mode 100644 index 0000000..c806ea0 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/rei/SkyblockCraftingRecipeDynamicGenerator.kt @@ -0,0 +1,52 @@ +package moe.nea.firmament.rei + +import io.github.moulberry.repo.data.NEUCraftingRecipe +import io.github.moulberry.repo.data.NEUForgeRecipe +import io.github.moulberry.repo.data.NEUItem +import io.github.moulberry.repo.data.NEURecipe +import java.util.* +import me.shedaniel.rei.api.client.registry.display.DynamicDisplayGenerator +import me.shedaniel.rei.api.client.view.ViewSearchBuilder +import me.shedaniel.rei.api.common.display.Display +import me.shedaniel.rei.api.common.entry.EntryStack +import moe.nea.firmament.recipes.SBCraftingRecipe +import moe.nea.firmament.recipes.SBForgeRecipe +import moe.nea.firmament.repo.RepoManager +import moe.nea.firmament.util.skyblockId + + +val SkyblockCraftingRecipeDynamicGenerator = neuDisplayGenerator<SBCraftingRecipe, NEUCraftingRecipe> { + SBCraftingRecipe(it) +} + +val SkyblockForgeRecipeDynamicGenerator = neuDisplayGenerator<SBForgeRecipe, NEUForgeRecipe> { + SBForgeRecipe(it) +} + +inline fun <D : Display, reified T : NEURecipe> neuDisplayGenerator(noinline mapper: (T) -> D) = + object : DynamicDisplayGenerator<D> { + override fun getRecipeFor(entry: EntryStack<*>): Optional<List<D>> { + if (entry.type != SBItemEntryDefinition.type) return Optional.empty() + val item = entry.castValue<NEUItem>() + val recipes = RepoManager.getRecipesFor(item.skyblockId) + val craftingRecipes = recipes.filterIsInstance<T>() + return Optional.of(craftingRecipes.map(mapper)) + } + + override fun generate(builder: ViewSearchBuilder): Optional<List<D>> { + if (SBCraftingRecipe.Category.catIdentifier !in builder.categories) return Optional.empty() + return Optional.of( + RepoManager.getAllRecipes().filterIsInstance<T>().map(mapper) + .toList() + ) + } + + override fun getUsageFor(entry: EntryStack<*>): Optional<List<D>> { + if (entry.type != SBItemEntryDefinition.type) return Optional.empty() + val item = entry.castValue<NEUItem>() + val recipes = RepoManager.getUsagesFor(item.skyblockId) + val craftingRecipes = recipes.filterIsInstance<T>() + return Optional.of(craftingRecipes.map(mapper)) + + } + } diff --git a/src/main/kotlin/moe/nea/firmament/rei/SkyblockItemIdFocusedStackProvider.kt b/src/main/kotlin/moe/nea/firmament/rei/SkyblockItemIdFocusedStackProvider.kt new file mode 100644 index 0000000..c874fc3 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/rei/SkyblockItemIdFocusedStackProvider.kt @@ -0,0 +1,25 @@ +package moe.nea.firmament.rei + +import dev.architectury.event.CompoundEventResult +import me.shedaniel.math.Point +import me.shedaniel.rei.api.client.registry.screen.FocusedStackProvider +import me.shedaniel.rei.api.common.entry.EntryStack +import moe.nea.firmament.mixins.accessor.AccessorHandledScreen +import moe.nea.firmament.repo.RepoManager +import moe.nea.firmament.util.skyBlockId +import net.minecraft.client.gui.screen.Screen +import net.minecraft.client.gui.screen.ingame.HandledScreen + +object SkyblockItemIdFocusedStackProvider : FocusedStackProvider { + override fun provide(screen: Screen?, mouse: Point?): CompoundEventResult<EntryStack<*>> { + if (screen !is HandledScreen<*>) return CompoundEventResult.pass() + screen as AccessorHandledScreen + val focusedSlot = screen.focusedSlot_NEU ?: return CompoundEventResult.pass() + val item = focusedSlot.stack ?: return CompoundEventResult.pass() + val skyblockId = item.skyBlockId ?: return CompoundEventResult.pass() + val neuItem = RepoManager.getNEUItem(skyblockId) ?: return CompoundEventResult.interrupt(false, null) + return CompoundEventResult.interruptTrue(EntryStack.of(SBItemEntryDefinition, neuItem)) + } + + override fun getPriority(): Double = 1_000_000.0 +} |