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 ab9c82344..4d8bd96b8 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 @@ -24,7 +24,7 @@ fun addCraftingTableRecipes(consumer: RecipeOutput) { val machinesCategory = RecipeCategory.DECORATIONS MatteryRecipe(MRegistry.CARGO_CRATES.item, category = RecipeCategory.DECORATIONS) - .row(MItemTags.TRITANIUM_PLATES, Tags.Items.CHESTS, MItemTags.TRITANIUM_PLATES) + .row(MItemTags.TRITANIUM_PLATES, multiIngredient(Tags.Items.CHESTS_WOODEN, Tags.Items.BARRELS_WOODEN), MItemTags.TRITANIUM_PLATES) .unlockedBy(MItemTags.TRITANIUM_PLATES) .unlockedBy(Tags.Items.CHESTS) .build(consumer) diff --git a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/MatteryRecipeProvider.kt b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/MatteryRecipeProvider.kt index 60ca7a37a..7110f8f93 100644 --- a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/MatteryRecipeProvider.kt +++ b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/MatteryRecipeProvider.kt @@ -19,6 +19,7 @@ import net.minecraft.tags.TagKey import net.minecraft.util.valueproviders.ConstantFloat import net.minecraft.util.valueproviders.FloatProvider import net.minecraft.world.item.Item +import net.minecraft.world.item.ItemStack import net.minecraft.world.item.crafting.Ingredient import net.minecraft.world.level.ItemLike import ru.dbotthepony.mc.otm.core.registryName @@ -52,6 +53,20 @@ fun inventoryTrigger(vararg p_126012_: ItemPredicate): Criterion() + + for (item in items) { + if (item is ItemStack) { + values.add(Ingredient.ItemValue(item)) + } else if (item is TagKey<*>) { + values.add(Ingredient.TagValue(item as TagKey)) + } + } + + return Ingredient.fromValues(values.stream()) +} + fun T.unlockedBy(item: ItemLike): T { val location = item.asItem().registryName!! unlockedBy("has_${location.namespace}_${location.path}", has(item)) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/ChestUpgraderItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/ChestUpgraderItem.kt new file mode 100644 index 000000000..6c07356ab --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/ChestUpgraderItem.kt @@ -0,0 +1,88 @@ +package ru.dbotthepony.mc.otm.item + +import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap +import net.minecraft.server.level.ServerLevel +import net.minecraft.world.Container +import net.minecraft.world.InteractionHand +import net.minecraft.world.InteractionResult +import net.minecraft.world.item.BlockItem +import net.minecraft.world.item.Item +import net.minecraft.world.item.ItemStack +import net.minecraft.world.item.context.BlockPlaceContext +import net.minecraft.world.item.context.UseOnContext +import net.minecraft.world.level.block.Block +import net.minecraft.world.level.block.entity.BarrelBlockEntity +import net.minecraft.world.level.block.entity.ChestBlockEntity +import net.minecraft.world.level.storage.loot.LootParams +import net.minecraft.world.level.storage.loot.parameters.LootContextParams +import ru.dbotthepony.mc.otm.block.decorative.CargoCrateBlock +import ru.dbotthepony.mc.otm.block.entity.decorative.CargoCrateBlockEntity +import ru.dbotthepony.mc.otm.core.math.Vector + +class ChestUpgraderItem : Item(Properties().stacksTo(1)) { + override fun onItemUseFirst(stack: ItemStack, context: UseOnContext): InteractionResult { + val player = context.player ?: return super.onItemUseFirst(stack, context) + + val otherHand = if (context.hand == InteractionHand.MAIN_HAND) InteractionHand.OFF_HAND else InteractionHand.MAIN_HAND + val crateStack = player.getItemInHand(otherHand) ?: return super.onItemUseFirst(stack, context) + + val block : Block + if (!crateStack.isEmpty && crateStack.item is BlockItem && (crateStack.item as BlockItem).block is CargoCrateBlock) { + block = (crateStack.item as BlockItem).block + } else { + return super.onItemUseFirst(stack, context) + } + + val pos = context.clickedPos + val hitBlockEntity = context.level.getBlockEntity(pos) + + if (hitBlockEntity is ChestBlockEntity || hitBlockEntity is BarrelBlockEntity) { + val container = hitBlockEntity as Container + + if (container.containerSize >= 54) return super.onItemUseFirst(stack, context) + + if (context.level is ServerLevel) { + val newState = block.getStateForPlacement(BlockPlaceContext(context)) ?: return InteractionResult.FAIL + + val contents = Int2ObjectArrayMap(container.containerSize) + for (i in 0 until container.containerSize) { + contents.put(i, container.getItem(i)) + } + container.clearContent() + + val level = context.level as ServerLevel + + val blockState = level.getBlockState(pos) + val lootparams = LootParams.Builder(level) + .withParameter(LootContextParams.ORIGIN, Vector.atCenterOf(context.clickedPos)) + .withParameter(LootContextParams.TOOL, stack) + .withOptionalParameter(LootContextParams.BLOCK_ENTITY, hitBlockEntity) + .withOptionalParameter(LootContextParams.BLOCK_STATE, blockState) + .withOptionalParameter(LootContextParams.THIS_ENTITY, context.player) + + blockState.spawnAfterBreak(level, pos, stack, true) + blockState.getDrops(lootparams).forEach { Block.popResource(level, pos, it) } + + if (!player.isCreative) + player.getItemInHand(otherHand).shrink(1) + + level.setBlockAndUpdate(pos, newState) + + val newBlockEntity = level.getBlockEntity(pos) + if (newBlockEntity is CargoCrateBlockEntity) { + for (i in 0 until contents.size) { + newBlockEntity.container[i] = contents.get(i) + } + } else { + for (i in 0 until contents.size) { + Block.popResource(level, pos, contents.get(i)) + } + } + } + + return InteractionResult.SUCCESS + } + + return super.onItemUseFirst(stack, context) + } +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MCreativeTabs.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MCreativeTabs.kt index 6f10f057f..e0d083627 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MCreativeTabs.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MCreativeTabs.kt @@ -169,6 +169,8 @@ private fun addMainCreativeTabItems(consumer: CreativeModeTab.Output) { accept(MItems.BLACK_HOLE) accept(MItems.GRAVITATIONAL_DISRUPTOR) + accept(MItems.CHEST_UPGRADER) + accept(MItems.ESSENCE_SERVO) energized(MItems.ALL_BATTERIES) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MItems.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MItems.kt index 39e6f0dd3..100f61660 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MItems.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MItems.kt @@ -566,6 +566,8 @@ object MItems { val METAL_JUNK: Item by registry.register(MNames.METAL_JUNK) { BlockItem(MBlocks.METAL_JUNK, DEFAULT_PROPERTIES) } val METAL_MESH: Item by registry.register(MNames.METAL_MESH) { BlockItem(MBlocks.METAL_MESH, DEFAULT_PROPERTIES) } + val CHEST_UPGRADER: Item by registry.register(MNames.CHEST_UPGRADER) { ChestUpgraderItem() } + init { MRegistry.INDUSTRIAL_GLASS.registerItems(registry) MRegistry.INDUSTRIAL_GLASS_PANE.registerItems(registry) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MNames.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MNames.kt index 8fac6557d..53e56b58d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MNames.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MNames.kt @@ -158,6 +158,8 @@ object MNames { const val PLASMA_RIFLE = "plasma_rifle" + const val CHEST_UPGRADER = "chest_upgrader" + // items: crafting components const val TRITANIUM_DUST = "tritanium_dust" const val TRITANIUM_NUGGET = "tritanium_nugget"