chest upgrader test (#281) + multiingredient for datagen + crate recipe accepts barrels

This commit is contained in:
YuRaNnNzZZ 2023-10-24 12:13:52 +03:00
parent ff547f9a5e
commit 831e49c796
Signed by: YuRaNnNzZZ
GPG Key ID: 5F71738C85A6006D
6 changed files with 110 additions and 1 deletions

View File

@ -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)

View File

@ -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<InventoryChange
))
}
fun multiIngredient(vararg items: Any) : Ingredient {
val values = arrayListOf<Ingredient.Value>()
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<Item>))
}
}
return Ingredient.fromValues(values.stream())
}
fun <T : RecipeBuilder> T.unlockedBy(item: ItemLike): T {
val location = item.asItem().registryName!!
unlockedBy("has_${location.namespace}_${location.path}", has(item))

View File

@ -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<ItemStack>(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)
}
}

View File

@ -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)

View File

@ -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)

View File

@ -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"