diff --git a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/CraftingTableRecipes.kt b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/CraftingTableRecipes.kt index ede9927e6..c1249bf27 100644 --- a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/CraftingTableRecipes.kt +++ b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/CraftingTableRecipes.kt @@ -205,4 +205,12 @@ fun addCraftingTableRecipes(consumer: Consumer) { .row(MItemTags.PLATE_TRITANIUM, MItems.MACHINE_FRAME, MItemTags.PLATE_TRITANIUM) .row(MItemTags.PLATE_TRITANIUM, MItems.ELECTRIC_PARTS, MItemTags.PLATE_TRITANIUM) .build(consumer) + + // Энерго меч + MatteryRecipe(MItems.ENERGY_SWORD) + .rowBC(MItemTags.PLATE_TRITANIUM, MItemTags.GOLD_WIRES) + .rowBC(MItemTags.PLATE_TRITANIUM, MItemTags.GOLD_WIRES) + .row(MItems.BATTERY_CAPACITOR, MItems.TRITANIUM_SWORD, MItemTags.ADVANCED_CIRCUIT) + .unlockedBy(MItems.BATTERY_CAPACITOR) + .buildEnergetic(consumer) } diff --git a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/MatteryRecipe.kt b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/MatteryRecipe.kt index 898f2760b..2f7c4ad6d 100644 --- a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/MatteryRecipe.kt +++ b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/MatteryRecipe.kt @@ -9,9 +9,11 @@ import net.minecraft.resources.ResourceLocation import net.minecraft.tags.TagKey import net.minecraft.world.item.Item import net.minecraft.world.item.crafting.Ingredient +import net.minecraft.world.item.crafting.RecipeSerializer import net.minecraft.world.level.ItemLike import ru.dbotthepony.mc.otm.OverdriveThatMatters import ru.dbotthepony.mc.otm.core.registryName +import ru.dbotthepony.mc.otm.recipe.EnergyContainerRecipe import java.util.function.Consumer private interface RecipeCell { @@ -156,6 +158,16 @@ class MatteryRecipe(val result: ItemLike, val count: Int = 1) { } } + fun buildEnergetic(consumer: Consumer, name: String? = null) { + build({ + consumer.accept(object : FinishedRecipe by it { + override fun getType(): RecipeSerializer<*> { + return EnergyContainerRecipe.Companion + } + }) + }, name) + } + fun row(): MatteryRecipe { if (index == 3) { throw IllegalStateException("Already have all rows defined") diff --git a/src/main/java/ru/dbotthepony/mc/otm/registry/MRecipes.java b/src/main/java/ru/dbotthepony/mc/otm/registry/MRecipes.java index d90b6b9ad..1f4d002cb 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/registry/MRecipes.java +++ b/src/main/java/ru/dbotthepony/mc/otm/registry/MRecipes.java @@ -5,10 +5,10 @@ import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.RecipeSerializer; import net.minecraft.world.item.crafting.RecipeType; import net.minecraftforge.eventbus.api.IEventBus; -import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries; import ru.dbotthepony.mc.otm.OverdriveThatMatters; +import ru.dbotthepony.mc.otm.recipe.EnergyContainerRecipe; import ru.dbotthepony.mc.otm.recipe.PlatePressRecipe; import ru.dbotthepony.mc.otm.recipe.PlatePressRecipeFactory; @@ -27,17 +27,20 @@ public class MRecipes { } public static final MatteryRecipeType PLATE_PRESS = new MatteryRecipeType<>(OverdriveThatMatters.loc(MNames.PLATE_PRESS)); + public static final MatteryRecipeType ENERGY_CONTAINER = new MatteryRecipeType<>(OverdriveThatMatters.loc("energy_container")); - private static final DeferredRegister> registry = DeferredRegister.create(ForgeRegistries.RECIPE_SERIALIZERS, OverdriveThatMatters.MOD_ID); - private static final DeferredRegister> typesRegistry = DeferredRegister.create(ForgeRegistries.RECIPE_TYPES, OverdriveThatMatters.MOD_ID); + private static final DeferredRegister> serializerRegistry = DeferredRegister.create(ForgeRegistries.RECIPE_SERIALIZERS, OverdriveThatMatters.MOD_ID); + private static final DeferredRegister> typeRegistry = DeferredRegister.create(ForgeRegistries.RECIPE_TYPES, OverdriveThatMatters.MOD_ID); static { - registry.register(MNames.PLATE_PRESS, () -> PlatePressRecipeFactory.INSTANCE); - typesRegistry.register(MNames.PLATE_PRESS, () -> PLATE_PRESS); + serializerRegistry.register(MNames.PLATE_PRESS, () -> PlatePressRecipeFactory.INSTANCE); + serializerRegistry.register(ENERGY_CONTAINER.name.getPath(), () -> EnergyContainerRecipe.Companion); + typeRegistry.register(MNames.PLATE_PRESS, () -> PLATE_PRESS); + typeRegistry.register(ENERGY_CONTAINER.name.getPath(), () -> ENERGY_CONTAINER); } public static void register(IEventBus bus) { - registry.register(bus); - typesRegistry.register(bus); + serializerRegistry.register(bus); + typeRegistry.register(bus); } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerSpliterator.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerSpliterator.kt new file mode 100644 index 000000000..c22763bb5 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerSpliterator.kt @@ -0,0 +1,25 @@ +package ru.dbotthepony.mc.otm.container + +import it.unimi.dsi.fastutil.objects.ObjectSpliterator +import it.unimi.dsi.fastutil.objects.ObjectSpliterators +import net.minecraft.world.Container +import net.minecraft.world.item.ItemStack +import java.util.stream.Stream +import java.util.stream.StreamSupport + +class ContainerSpliterator(private val container: Container, offset: Int = 0, private val maxPos: Int = container.containerSize) : ObjectSpliterators.AbstractIndexBasedSpliterator(offset) { + override fun get(location: Int): ItemStack { + return container[location] + } + + override fun getMaxPos(): Int { + return maxPos + } + + override fun makeForSplit(pos: Int, maxPos: Int): ObjectSpliterator { + return ContainerSpliterator(container, pos, maxPos) + } +} + +fun Container.spliterator() = ContainerSpliterator(this) +fun Container.stream(): Stream = StreamSupport.stream(spliterator(), false) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/EnergySwordItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/EnergySwordItem.kt index da1d4fc37..39238bc99 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/EnergySwordItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/EnergySwordItem.kt @@ -160,10 +160,10 @@ class EnergySwordItem : Item(Properties().stacksTo(1).rarity(Rarity.RARE).tab(Ov } companion object { - private val MAX_ENERGY = ImpreciseFraction(400_000) - private val ENERGY_ZAP = ImpreciseFraction(10_000) - private val ENERGY_PER_SWING = ImpreciseFraction(4_000) - private val COBWEB_POWER_COST = ImpreciseFraction(1_500) + private val MAX_ENERGY = ImpreciseFraction(500_000) + private val ENERGY_ZAP = ImpreciseFraction(4_000) + private val ENERGY_PER_SWING = ImpreciseFraction(2_000) + private val COBWEB_POWER_COST = ImpreciseFraction(2_500) private val PLANT_POWER_COST = ImpreciseFraction(500) private val DESCRIPTION = TranslatableComponent("item.overdrive_that_matters.energy_sword.desc").withStyle(ChatFormatting.DARK_GRAY) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/EnergyContainerRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/EnergyContainerRecipe.kt new file mode 100644 index 000000000..42688d380 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/EnergyContainerRecipe.kt @@ -0,0 +1,56 @@ +package ru.dbotthepony.mc.otm.recipe + +import com.google.gson.JsonObject +import net.minecraft.network.FriendlyByteBuf +import net.minecraft.resources.ResourceLocation +import net.minecraft.world.inventory.CraftingContainer +import net.minecraft.world.item.ItemStack +import net.minecraft.world.item.crafting.CraftingRecipe +import net.minecraft.world.item.crafting.Recipe +import net.minecraft.world.item.crafting.RecipeSerializer +import net.minecraft.world.item.crafting.RecipeType +import net.minecraft.world.item.crafting.ShapedRecipe +import ru.dbotthepony.mc.otm.capability.ItemEnergyStorageImpl +import ru.dbotthepony.mc.otm.capability.matteryEnergy +import ru.dbotthepony.mc.otm.container.stream +import ru.dbotthepony.mc.otm.core.set +import ru.dbotthepony.mc.otm.core.tagNotNull + +class EnergyContainerRecipe( + val parent: ShapedRecipe +) : CraftingRecipe by parent { + override fun assemble(container: CraftingContainer): ItemStack { + if (container.stream().anyMatch { it.isDamaged }) { + return ItemStack.EMPTY + } + + val itemStack = parent.assemble(container) + + val battery = container.stream() + .filter { !it.isEmpty } + .map { it.matteryEnergy } + .filter { it != null } + .findAny().orElse(null) + + if (battery != null) { + itemStack.tagNotNull[ItemEnergyStorageImpl.NBT_KEY] = battery.batteryLevel.serializeNBT() + } + + return itemStack + } + + companion object : RecipeSerializer { + override fun fromJson(p_44103_: ResourceLocation, p_44104_: JsonObject): EnergyContainerRecipe { + return EnergyContainerRecipe(ShapedRecipe.Serializer.SHAPED_RECIPE.fromJson(p_44103_, p_44104_)) + } + + override fun fromNetwork(p_44105_: ResourceLocation, p_44106_: FriendlyByteBuf): EnergyContainerRecipe? { + return ShapedRecipe.Serializer.SHAPED_RECIPE.fromNetwork(p_44105_, p_44106_)?.let(::EnergyContainerRecipe) + } + + override fun toNetwork(p_44101_: FriendlyByteBuf, p_44102_: EnergyContainerRecipe) { + ShapedRecipe.Serializer.SHAPED_RECIPE.toNetwork(p_44101_, p_44102_.parent) + } + } + +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PlatePressRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PlatePressRecipe.kt index 6d23fc1e4..bde482193 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PlatePressRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PlatePressRecipe.kt @@ -34,44 +34,30 @@ class PlatePressRecipe( return input.test(container[PlatePressBlockEntity.SLOT_INPUT]) } - private var outputStack: ItemStack? = null + private val outputStack: ItemStack by lazy { + if (output.isEmpty || input.isEmpty) { + ItemStack.EMPTY + } else { + val items = output.items + val bestMatch = items.firstOrNull { it.item.registryName?.namespace == OverdriveThatMatters.MOD_ID } ?: items[0] + bestMatch.copy().also { it.count = this.count } + } + } override fun getIngredients(): NonNullList { if (input.isEmpty || output.isEmpty) return super.getIngredients() - return NonNullList.of(null, input) + return NonNullList.of(Ingredient.EMPTY, input) } override fun isIncomplete(): Boolean { return !(input.items.any { it.item != Items.BARRIER } && output.items.any { it.item != Items.BARRIER }) } - override fun assemble(p_44001_: Container): ItemStack = resultItem.copy() + override fun assemble(p_44001_: Container): ItemStack = outputStack.copy() override fun canCraftInDimensions(p_43999_: Int, p_44000_: Int) = true - override fun getResultItem(): ItemStack { - if (outputStack == null) { - if (output.isEmpty || input.isEmpty) { - outputStack = ItemStack.EMPTY - } else { - val items = output.items - var bestMatch = items[0] - - // прежде всего выдавать предметы из OTM - for (item in items) { - if (item.item.registryName?.namespace == OverdriveThatMatters.MOD_ID) { - bestMatch = item - break - } - } - - outputStack = bestMatch.copy() - outputStack!!.count = this.count - } - } - - return outputStack!! - } + override fun getResultItem(): ItemStack = outputStack override fun getId() = id