Skip to content

Commit

Permalink
working toaster (not finished but whatever)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttttdoy committed Jul 11, 2024
1 parent 043930d commit a5181d6
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 66 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ dependencies {
implementation fg.deobf("com.simibubi.create:create-${minecraft_version}:${create_version}:slim") { transitive = false }
implementation fg.deobf("com.jozufozu.flywheel:flywheel-forge-${minecraft_version}:${flywheel_version}")
implementation fg.deobf("com.tterrag.registrate:Registrate:${registrate_version}")
runtimeOnly fg.deobf("curse.maven:embeddium-908741:5503379")

runtimeOnly(fg.deobf("curse.maven:pipez-${pipez_project_id}:${pipez_file_id}"))
implementation(fg.deobf("curse.maven:jade-${jade_project_id}:${jade_file_id}"))
Expand Down
112 changes: 93 additions & 19 deletions src/main/kotlin/breadmod/block/machine/ToasterBlock.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package breadmod.block.machine

import breadmod.ModMain
import breadmod.block.machine.entity.ToasterBlockEntity
import breadmod.registry.block.ModBlockEntities
import breadmod.registry.item.ModItems
import breadmod.util.capability.IndexableItemHandler
import net.minecraft.ChatFormatting
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.core.particles.ParticleTypes
import net.minecraft.network.chat.Component
import net.minecraft.sounds.SoundEvents
import net.minecraft.sounds.SoundSource
Expand All @@ -15,6 +17,8 @@ import net.minecraft.world.InteractionHand
import net.minecraft.world.InteractionResult
import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import net.minecraft.world.item.TooltipFlag
import net.minecraft.world.item.context.BlockPlaceContext
import net.minecraft.world.level.BlockGetter
import net.minecraft.world.level.Level
Expand All @@ -25,6 +29,7 @@ import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.block.state.StateDefinition
import net.minecraft.world.level.block.state.properties.BlockStateProperties
import net.minecraft.world.level.block.state.properties.DirectionProperty
import net.minecraft.world.level.block.state.properties.Property
import net.minecraft.world.level.material.MapColor
import net.minecraft.world.phys.BlockHitResult
import net.minecraft.world.phys.shapes.CollisionContext
Expand All @@ -34,7 +39,7 @@ import net.minecraftforge.common.capabilities.ForgeCapabilities
class ToasterBlock : BaseAbstractMachineBlock.Powered<ToasterBlockEntity>(
ModBlockEntities.TOASTER,
Properties.of()
.strength(1f, 3.0f)
.strength(1f, 1.0f)
.mapColor(MapColor.TERRACOTTA_WHITE)
.sound(SoundType.COPPER),
false
Expand All @@ -43,20 +48,21 @@ class ToasterBlock : BaseAbstractMachineBlock.Powered<ToasterBlockEntity>(
private val aabbZ = box(2.0, 0.0, 5.0, 14.0, 7.0, 11.0)
private val facing: DirectionProperty = BlockStateProperties.HORIZONTAL_FACING
private val random = RandomSource.create()
private val triggered = BlockStateProperties.TRIGGERED

override fun canHarvestBlock(pState: BlockState, pLevel: BlockGetter, pPos: BlockPos, pPlayer: Player): Boolean =
!pPlayer.isCreative
override fun getStateForPlacement(pContext: BlockPlaceContext): BlockState =
defaultBlockState()
.setValue(BlockStateProperties.HORIZONTAL_FACING, pContext.horizontalDirection.opposite)
.setValue(BlockStateProperties.TRIGGERED, false)
.setValue(triggered, false)
.setValue(BlockStateProperties.WATERLOGGED, false)
override fun adjustBlockStateDefinition(pBuilder: StateDefinition.Builder<Block, BlockState>) {
pBuilder.add(BlockStateProperties.HORIZONTAL_FACING, BlockStateProperties.TRIGGERED)
}

init {
registerDefaultState(defaultBlockState().setValue(BlockStateProperties.TRIGGERED, false))
// todo probably don't need this since the state is set on block placement
pBuilder.add(
BlockStateProperties.HORIZONTAL_FACING,
BlockStateProperties.TRIGGERED,
BlockStateProperties.WATERLOGGED
)
}

@Deprecated("Deprecated in Java")
Expand All @@ -72,6 +78,8 @@ class ToasterBlock : BaseAbstractMachineBlock.Powered<ToasterBlockEntity>(
}
}

// todo yeeterson idea, waterlogged toaster that will zap and kill you if you activate it
// todo (figure out lightning particle or model that starts from the toaster to the player)
@Deprecated("Deprecated in Java")
override fun use(
pState: BlockState,
Expand All @@ -82,35 +90,93 @@ class ToasterBlock : BaseAbstractMachineBlock.Powered<ToasterBlockEntity>(
pHit: BlockHitResult
): InteractionResult {
if(!pLevel.isClientSide) {
val triggered = pState.getValue(BlockStateProperties.TRIGGERED)
val triggeredState = pState.getValue(triggered)
val entity = (pLevel.getBlockEntity(pPos) as? ToasterBlockEntity) ?: return InteractionResult.FAIL
val itemHandler = entity.capabilityHolder.capabilityOrNull<IndexableItemHandler>(ForgeCapabilities.ITEM_HANDLER) ?: return InteractionResult.FAIL
val itemHandlerSlot = itemHandler.getStackInSlot(0)
val stack = pPlayer.getItemInHand(pHand)
val item = stack.item

if(pPlayer.isCrouching && pHand != InteractionHand.OFF_HAND && entity.progress == 0) {
if(triggered && entity.progress == 0) {
pLevel.setBlockAndUpdate(pPos, pState.setValue(BlockStateProperties.TRIGGERED, false))
pPlayer.sendSystemMessage(Component.literal("toaster not triggered"))
} else if(!triggered && entity.progress == 0) {
pLevel.setBlockAndUpdate(pPos, pState.setValue(BlockStateProperties.TRIGGERED, true))
pPlayer.sendSystemMessage(Component.literal("toaster triggered"))
if(triggeredState && entity.progress == 0) {
updateState(pLevel, pPos, pState, triggered, false)
} else if(!triggeredState && entity.progress == 0) {
updateState(pLevel, pPos, pState, triggered, true)
}
}

if(item == ModItems.BREAD_SLICE.get().asItem() &&
itemHandler[0].count < 2 && !pPlayer.isCrouching && !triggered && entity.progress == 0) {
if(!pPlayer.isCrouching && !triggeredState && entity.progress == 0 && !stack.isEmpty && itemHandlerSlot.count != 2) {
if(!pPlayer.isCreative) stack.shrink(1)
itemHandler.insertItem(ItemStack(item, 1), false)
pLevel.playSound(null, pPos, SoundEvents.ITEM_PICKUP, SoundSource.BLOCKS, 0.2f, random.nextFloat()-0.3f)
} else if(!pPlayer.isCrouching && !triggered && entity.progress == 0) {
entity.setChanged()
} else if(!pPlayer.isCrouching && !triggeredState && entity.progress == 0) {
Containers.dropContents(pLevel, pPos, entity)
entity.setChanged()
}
}

return InteractionResult.sidedSuccess(pLevel.isClientSide)
}

override fun animateTick(pState: BlockState, pLevel: Level, pPos: BlockPos, pRandom: RandomSource) {
val entity = pLevel.getBlockEntity(pPos) as? ToasterBlockEntity ?: return
val itemHandler = entity.capabilityHolder.capabilityOrNull<IndexableItemHandler>(ForgeCapabilities.ITEM_HANDLER) ?: return
val itemHandlerSlot = itemHandler.getStackInSlot(0)
val posX = pPos.x + 0.4
val posY = pPos.y + 0.5
val posZ = pPos.z + 0.5

val direction = pState.getValue(facing)
val axis = direction.axis
val d1 = pRandom.nextDouble() * 0.6 - 0.3
val d4 = if(axis == Direction.Axis.X) direction.stepZ * 0.52 else d1 // X
val d3 = pRandom.nextDouble() * 0.6 / 16.0 // Y
val d2 = if(axis == Direction.Axis.Z) direction.stepX * 0.52 else d1 // Z
if(pState.getValue(triggered)) {
if(itemHandlerSlot.`is`(Items.CHARCOAL)) {
pLevel.addParticle(
ParticleTypes.LAVA,
posX + d2,
posY + d3,
posZ + d4 + if(axis == Direction.Axis.X) -0.1 else 0.0,
0.0, 0.0, 0.0
)
pLevel.addParticle(
ParticleTypes.LAVA,
posX + d2,
posY + d3,
posZ + d4 + if(axis == Direction.Axis.X) 0.1 else 0.0,
0.0, 0.0, 0.0
)
} else {
pLevel.addParticle(
ParticleTypes.SMOKE,
posX + d2,
posY + d3,
posZ + d4 + if(axis == Direction.Axis.X) -0.1 else 0.0,
0.0, 0.0, 0.0
)
pLevel.addParticle(
ParticleTypes.SMOKE,
posX + d2 + 0.2,
posY + d3,
posZ + d4 + if(axis == Direction.Axis.X) 0.1 else 0.0,
0.0, 0.0, 0.0
)
}
}
}

override fun appendHoverText(
pStack: ItemStack,
pLevel: BlockGetter?,
pTooltip: MutableList<Component>,
pFlag: TooltipFlag
) {
pTooltip.add(1, ModMain.modTranslatable("toaster", "tooltip").withStyle(ChatFormatting.RED))
}

@Deprecated("Deprecated in Java", ReplaceWith(
"super.onRemove(pState, pLevel, pPos, pNewState, pMovedByPiston)",
"net.minecraft.world.level.block.Block"
Expand All @@ -132,4 +198,12 @@ class ToasterBlock : BaseAbstractMachineBlock.Powered<ToasterBlockEntity>(

override fun getServerTicker(pLevel: Level, pState: BlockState): BlockEntityTicker<ToasterBlockEntity> =
BlockEntityTicker { tLevel, tPos, tState, tBlockEntity -> tBlockEntity.tick(tLevel, tPos, tState, tBlockEntity) }

private fun <T: Comparable<T>, V:T> updateState(
pLevel: Level,
pPos: BlockPos,
pState: BlockState,
pValue: Property<T>,
boolean: V
) = pLevel.setBlockAndUpdate(pPos, pState.setValue(pValue, boolean))
}
73 changes: 36 additions & 37 deletions src/main/kotlin/breadmod/block/machine/entity/ToasterBlockEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import breadmod.util.capability.IndexableItemHandler
import breadmod.util.capability.StorageDirection
import net.minecraft.core.BlockPos
import net.minecraft.nbt.CompoundTag
import net.minecraft.sounds.SoundEvents
import net.minecraft.sounds.SoundSource
import net.minecraft.world.entity.player.Player
import net.minecraft.world.entity.player.StackedContents
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.block.state.properties.BlockStateProperties
Expand All @@ -26,9 +29,11 @@ class ToasterBlockEntity(
pBlockState,
ModRecipeTypes.TOASTING,
ForgeCapabilities.ITEM_HANDLER to (IndexableItemHandler(listOf(
2 to StorageDirection.BIDIRECTIONAL
2 to StorageDirection.STORE_ONLY
)) to null)
) {
private val triggered = BlockStateProperties.TRIGGERED

override fun adjustSaveAdditionalProgressive(pTag: CompoundTag) {
super.adjustSaveAdditionalProgressive(pTag)
pTag.put(ModMain.ID, CompoundTag().also { dataTag ->
Expand All @@ -53,31 +58,40 @@ class ToasterBlockEntity(
pState: BlockState,
pBlockEntity: Progressive<ToasterBlockEntity, ToasterRecipe>
) {
val triggered = pBlockEntity.blockState.getValue(BlockStateProperties.TRIGGERED)
println("ticking")
if(triggered) {
val triggeredState = pBlockEntity.blockState.getValue(triggered)
if(triggeredState) {
preTick(pLevel, pPos, pState, pBlockEntity)
// todo attempt to figure out recipes with 1 slice and/or 2 slices as input
currentRecipe.ifPresentOrElse({
// the toaster does NOT like charcoal
if(getItemHandler()?.getStackInSlot(0)?.`is`(Items.CHARCOAL) == true) {
maxProgress = 60
progress ++
println("progress++")
recipeTick(pLevel, pPos, pState, pBlockEntity, it)
if (progress >= it.time && recipeDone(pLevel, pPos, pState, pBlockEntity, it)) {
currentRecipe = Optional.empty()
progress = 0; maxProgress = 0
pLevel.setBlockAndUpdate(pPos, pState.setValue(BlockStateProperties.TRIGGERED, false))
}
}, {
val recipe = recipeDial.getRecipeFor(this, pLevel)
recipe.ifPresent{
// consumeRecipe(pLevel, pPos, pState, pBlockEntity, it)
currentRecipe = recipe
maxProgress = it.time
if(progress == 35) pLevel.playSound(null, pPos, SoundEvents.TNT_PRIMED, SoundSource.BLOCKS)
if(progress >= 60) {
pLevel.explode(null, pPos.x.toDouble(), pPos.y.toDouble(), pPos.z.toDouble(), 3f, Level.ExplosionInteraction.BLOCK)
}
})
} else {
currentRecipe.ifPresentOrElse({
progress ++
recipeTick(pLevel, pPos, pState, pBlockEntity, it)
if (progress >= it.time && recipeDone(pLevel, pPos, pState, pBlockEntity, it)) {
currentRecipe = Optional.empty()
progress = 0; maxProgress = 0
pLevel.setBlockAndUpdate(pPos, pState.setValue(triggered, false))
pLevel.playSound(null, pPos, SoundEvents.NOTE_BLOCK_BELL.get(), SoundSource.BLOCKS, 0.2f, 0.8f)
}
}, {
val recipe = recipeDial.getRecipeFor(this, pLevel)
recipe.ifPresentOrElse({
currentRecipe = recipe
maxProgress = it.time
}, {
pLevel.setBlockAndUpdate(pPos, pState.setValue(triggered, false))
pLevel.playSound(null, pPos, SoundEvents.NOTE_BLOCK_BASS.get(), SoundSource.BLOCKS, 0.2f, 0.5f)
})
})
}
postTick(pLevel, pPos, pState, pBlockEntity)
println("state is triggered")
} else println("state is not triggered")
}
}

override fun recipeDone(
Expand All @@ -98,28 +112,13 @@ class ToasterBlockEntity(
} else false
}


// todo figure out why this is only showing the items *when* the recipe is triggered
fun getRenderStack(): ItemStack {
val item = getItemHandler()
return if(item?.getStackInSlot(0)?.isEmpty == false) {
item.getStackInSlot(0)
} else ItemStack.EMPTY
}

// override fun consumeRecipe(
// pLevel: Level,
// pPos: BlockPos,
// pState: BlockState,
// pBlockEntity: Progressive<ToasterBlockEntity, ToasterRecipe>,
// recipe: ToasterRecipe
// ): Boolean {
// val itemHandle = getItemHandler() ?: return false
// recipe.itemsRequired?.forEach { stack -> itemHandle[0].shrink(stack.count) }
// recipe.itemsRequiredTagged?.forEach { tag -> itemHandle[0].shrink(tag.second) }
// return true
// }

private fun getItemHandler() = capabilityHolder.capabilityOrNull<IndexableItemHandler>(ForgeCapabilities.ITEM_HANDLER)

override fun clearContent() { getItemHandler()?.clear() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import net.minecraft.client.renderer.blockentity.BlockEntityRenderer
import net.minecraft.client.renderer.texture.OverlayTexture
import net.minecraft.core.Direction
import net.minecraft.world.item.ItemDisplayContext
import net.minecraft.world.item.ItemStack
import net.minecraft.world.level.block.entity.BlockEntity
import net.minecraft.world.level.block.state.properties.BlockStateProperties
import net.minecraftforge.client.model.generators.ModelProvider

Expand All @@ -27,7 +29,6 @@ class ToasterRenderer: BlockEntityRenderer<ToasterBlockEntity> {
pPackedOverlay: Int
) {
val instance = Minecraft.getInstance()
val itemRenderer = instance.itemRenderer
val handleModel = instance.modelManager.getModel(handleModelLocation)
val blockRotation = pBlockEntity.blockState.getValue(BlockStateProperties.HORIZONTAL_FACING)
val triggered = pBlockEntity.blockState.getValue(BlockStateProperties.TRIGGERED)
Expand Down Expand Up @@ -56,11 +57,31 @@ class ToasterRenderer: BlockEntityRenderer<ToasterBlockEntity> {

renderBlockModel(pPoseStack, pBuffer, pBlockEntity, handleModel, pPackedLight, pPackedOverlay)
pPoseStack.popPose()
val itemStack = pBlockEntity.getRenderStack()

// todo set up 2 items in the up and down position depending on triggered state
pPoseStack.pushPose()
pPoseStack.translate(0.5, 0.3, 0.61)
pPoseStack.scale(0.6f, 0.6f, 0.6f)
if(blockRotation == Direction.SOUTH || blockRotation == Direction.NORTH) {
pPoseStack.mulPose(Axis.YN.rotationDegrees(90f))
pPoseStack.translate(-0.185, 0.0, 0.185)
}
if(!triggered) {
if(itemStack.count == 2) {
renderStaticItem(itemStack, pPoseStack, pBuffer, pBlockEntity, pPackedLight)
pPoseStack.translate(0.0, 0.0, -0.37)
renderStaticItem(itemStack, pPoseStack, pBuffer, pBlockEntity, pPackedLight)
} else {
renderStaticItem(itemStack, pPoseStack, pBuffer, pBlockEntity, pPackedLight)
}
}
pPoseStack.popPose()
}

private fun renderStaticItem(pStack: ItemStack, pPoseStack: PoseStack, pBuffer: MultiBufferSource, pBlockEntity: BlockEntity, pPackedLight: Int) {
val itemRenderer = Minecraft.getInstance().itemRenderer
itemRenderer.renderStatic(
pBlockEntity.getRenderStack(),
pStack,
ItemDisplayContext.FIXED,
pPackedLight,
OverlayTexture.NO_OVERLAY,
Expand All @@ -69,6 +90,5 @@ class ToasterRenderer: BlockEntityRenderer<ToasterBlockEntity> {
pBlockEntity.level,
1
)
pPoseStack.popPose()
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/breadmod/datagen/ModRecipeProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,11 @@ class ModRecipeProvider(pOutput: PackOutput) : RecipeProvider(pOutput) {
.requiresItem(ModItems.BREAD_SLICE.get(), 2)
.setSerializer(ModRecipeSerializers.TOASTER.get())
.save(pWriter, modLocation("special", "machine", "toaster", "toast"))
FluidEnergyRecipeBuilder(Items.CHARCOAL, 2)
.setTimeRequired(20 * 5)
.requiresItem(ModItems.TOAST.get(), 2)
.setSerializer(ModRecipeSerializers.TOASTER.get())
.save(pWriter, modLocation("special", "machine", "toaster", "burnt_toast"))

// // Compat
// Create
Expand Down
Loading

0 comments on commit a5181d6

Please sign in to comment.