Skip to content

Commit

Permalink
sleepy2
Browse files Browse the repository at this point in the history
  • Loading branch information
ATPStorages committed Jul 26, 2024
1 parent 4eed65e commit e789193
Show file tree
Hide file tree
Showing 29 changed files with 20,075 additions and 17,106 deletions.
31 changes: 31 additions & 0 deletions qodana.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#-------------------------------------------------------------------------------#
# Qodana analysis is configured by qodana.yaml file #
# https://www.jetbrains.com/help/qodana/qodana-yaml.html #
#-------------------------------------------------------------------------------#
version: "1.0"

#Specify inspection profile for code analysis
profile:
name: qodana.starter

#Enable inspections
#include:
# - name: <SomeEnabledInspectionId>

#Disable inspections
#exclude:
# - name: <SomeDisabledInspectionId>
# paths:
# - <path/where/not/run/inspection>

projectJDK: 17 #(Applied in CI/CD pipeline)

#Execute shell command before Qodana execution (Applied in CI/CD pipeline)
#bootstrap: sh ./prepare-qodana.sh

#Install IDE plugins before Qodana execution (Applied in CI/CD pipeline)
#plugins:
# - id: <plugin.id> #(plugin id can be found at https://plugins.jetbrains.com)

#Specify Qodana linter for analysis (Applied in CI/CD pipeline)
linter: jetbrains/qodana-jvm:latest
29 changes: 20 additions & 9 deletions src/main/kotlin/breadmod/block/machine/CraftingManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,48 @@ import net.minecraft.world.entity.player.StackedContents
import net.minecraft.world.inventory.CraftingContainer
import net.minecraft.world.item.ItemStack

class CraftingManager<T: AbstractMachineBlockEntity<*>>(
class CraftingManager<T : AbstractMachineBlockEntity<*>>(
private val itemHandler: IndexableItemHandler,
private val viewSlots: List<Int>,
private val width: Int,
private val height: Int,
val parent: T
): CraftingContainer {
) : CraftingContainer {
init {
require(viewSlots.size <= itemHandler.size) { "View slots size must not exceed item handler size" }
}

override fun clearContent() { viewSlots.forEach { itemHandler[it].count = 0 } }
override fun clearContent() {
viewSlots.forEach { itemHandler[it].count = 0 }
}

override fun getContainerSize(): Int = viewSlots.size
override fun isEmpty(): Boolean = viewSlots.sumOf { itemHandler[it].count } == 0

private fun contains(pSlot: Int) = viewSlots.contains(pSlot)

override fun getItem(pSlot: Int): ItemStack =
if(contains(pSlot)) itemHandler[pSlot] else ItemStack.EMPTY
if (contains(pSlot)) itemHandler.getOrNull(pSlot) ?: ItemStack.EMPTY else ItemStack.EMPTY

override fun removeItem(pSlot: Int, pAmount: Int): ItemStack =
if(contains(pSlot)) itemHandler[pSlot].split(pAmount) ?: ItemStack.EMPTY else ItemStack.EMPTY
if (contains(pSlot)) itemHandler[pSlot].split(pAmount) ?: ItemStack.EMPTY else ItemStack.EMPTY

override fun removeItemNoUpdate(pSlot: Int): ItemStack =
if(contains(pSlot)) itemHandler[pSlot].copyAndClear() ?: ItemStack.EMPTY else ItemStack.EMPTY
if (contains(pSlot)) itemHandler[pSlot].copyAndClear() ?: ItemStack.EMPTY else ItemStack.EMPTY

override fun setItem(pSlot: Int, pStack: ItemStack) {
if(contains(pSlot)) itemHandler[pSlot] = pStack }
if (contains(pSlot)) itemHandler[pSlot] = pStack
}

override fun stillValid(pPlayer: Player): Boolean = true
override fun fillStackedContents(pContents: StackedContents) {
itemHandler[0].let { pContents.accountStack(it) } }
itemHandler[0].let { pContents.accountStack(it) }
}

override fun getWidth(): Int = width
override fun getHeight(): Int = height
override fun getItems(): MutableList<ItemStack> = itemHandler.slice(viewSlots).toMutableList()
override fun setChanged() { itemHandler.changed?.invoke() }
override fun setChanged() {
itemHandler.changed?.invoke()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package breadmod.block.machine.entity

import breadmod.block.machine.CraftingManager
import breadmod.network.CapabilityDataPacket
import breadmod.network.PacketHandler.NETWORK
import breadmod.network.client.CapabilitySideDataPacket
import breadmod.network.client.CapabilityTagDataPacket
import breadmod.recipe.fluidEnergy.FluidEnergyRecipe
import breadmod.util.capability.*
import net.minecraft.core.BlockPos
Expand Down Expand Up @@ -41,20 +42,29 @@ abstract class AbstractMachineBlockEntity<T: AbstractMachineBlockEntity<T>>(
this.capabilityHolder.capabilities.forEach { (_, u) ->
u.first.ifPresent { it.changed = { setChanged() } }
}
this.capabilityHolder.changed = { cap, _, _ ->
NETWORK.send(
PacketDistributor.TRACKING_CHUNK.with { (level as ServerLevel).getChunkAt(blockPos) },
CapabilitySideDataPacket(blockPos, cap.name, capabilityHolder.capabilities[cap]?.second ?: emptyList())
)
}
}

final override fun <T : Any?> getCapability(cap: Capability<T>): LazyOptional<T> =
getCapability(cap, null)
override fun <T : Any?> getCapability(cap: Capability<T>, side: Direction?): LazyOptional<T>
= capabilityHolder.capabilitySided(cap, blockState.getValue(BlockStateProperties.HORIZONTAL_FACING), side)
?: super.getCapability(cap, side)

override fun <T : Any?> getCapability(cap: Capability<T>, side: Direction?): LazyOptional<T> =
capabilityHolder.capabilitySided(cap, blockState.getValue(BlockStateProperties.HORIZONTAL_FACING), side)
?: super.getCapability(cap, side)

final override fun invalidateCaps() {
capabilityHolder.invalidate()
super.invalidateCaps()
}

final override fun getUpdatePacket(): Packet<ClientGamePacketListener> =
ClientboundBlockEntityDataPacket.create(this)

final override fun getUpdateTag(): CompoundTag =
CompoundTag().also { saveAdditional(it) }

Expand Down Expand Up @@ -88,7 +98,7 @@ abstract class AbstractMachineBlockEntity<T: AbstractMachineBlockEntity<T>>(
adjustChanged()
if(level is ServerLevel) NETWORK.send(
PacketDistributor.TRACKING_CHUNK.with { (level as ServerLevel).getChunkAt(blockPos) },
CapabilityDataPacket(blockPos, updateTag)
CapabilityTagDataPacket(blockPos, updateTag)
)
super.setChanged()
}
Expand All @@ -112,7 +122,7 @@ abstract class AbstractMachineBlockEntity<T: AbstractMachineBlockEntity<T>>(
pPos: BlockPos,
pBlockState: BlockState,
private val recipeType: RegistryObject<RecipeType<R>>,
itemHandler: Pair<IndexableItemHandler, MutableList<Direction?>?>,
itemHandler: Pair<IndexableItemHandler, List<Direction?>>,
itemHandlerViewSlots: List<Int>,
craftingWidthHeight: Pair<Int, Int>,
vararg additionalCapabilities: Pair<Capability<*>, CapabilityContainer>
Expand Down Expand Up @@ -173,20 +183,33 @@ abstract class AbstractMachineBlockEntity<T: AbstractMachineBlockEntity<T>>(
maxProgress = pTag.getInt(MAX_PROGRESS_KEY)
}

open fun noRecipeTick (pLevel: Level, pPos: BlockPos, pState: BlockState, pBlockEntity: Progressive<T,R>) {}
open fun consumeRecipe(pLevel: Level, pPos: BlockPos, pState: BlockState, pBlockEntity: Progressive<T, R>, recipe: R): Boolean = true
open fun recipeTick (pLevel: Level, pPos: BlockPos, pState: BlockState, pBlockEntity: Progressive<T,R>, recipe: R) {}
open fun recipeDone (pLevel: Level, pPos: BlockPos, pState: BlockState, pBlockEntity: Progressive<T,R>, recipe: R): Boolean = true
open fun noRecipeTick(pLevel: Level, pPos: BlockPos, pState: BlockState, pBlockEntity: Progressive<T, R>) {}
open fun consumeRecipe(
pLevel: Level,
pPos: BlockPos,
pState: BlockState,
pBlockEntity: Progressive<T, R>,
recipe: R
): Boolean = true

open fun recipeTick(pLevel: Level, pPos: BlockPos, pState: BlockState, pBlockEntity: Progressive<T, R>, recipe: R) {}
open fun recipeDone(
pLevel: Level,
pPos: BlockPos,
pState: BlockState,
pBlockEntity: Progressive<T, R>,
recipe: R
): Boolean = true

abstract class Powered<T: AbstractMachineBlockEntity<T>, R: FluidEnergyRecipe>(
pType: BlockEntityType<T>,
pPos: BlockPos,
pBlockState: BlockState,
recipeType: RegistryObject<RecipeType<R>>,
itemHandler: Pair<IndexableItemHandler, MutableList<Direction?>?>,
itemHandler: Pair<IndexableItemHandler, List<Direction?>>,
itemHandlerViewSlots: List<Int>,
craftingWidthHeight: Pair<Int, Int>,
powerHandler: Pair<EnergyBattery, MutableList<Direction?>?>,
powerHandler: Pair<EnergyBattery, List<Direction?>>,
vararg additionalCapabilities: Pair<Capability<*>, CapabilityContainer>
): Progressive<T,R>(
pType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,21 @@ class DoughMachineBlockEntity(
pPos,
pBlockState,
ModRecipeTypes.DOUGH_MACHINE,
IndexableItemHandler(listOf(
64 to StorageDirection.STORE_ONLY,
64 to StorageDirection.EMPTY_ONLY,
1 to StorageDirection.BIDIRECTIONAL
)) to mutableListOf(null, Direction.WEST, Direction.EAST, Direction.DOWN),
IndexableItemHandler(
listOf(
64 to StorageDirection.STORE_ONLY,
64 to StorageDirection.EMPTY_ONLY,
1 to StorageDirection.BIDIRECTIONAL
)
) to mutableListOf(null, Direction.WEST, Direction.EAST, Direction.DOWN),
listOf(0),
1 to 1,
EnergyBattery(50000, 2000) to mutableListOf(null, Direction.NORTH),
ForgeCapabilities.FLUID_HANDLER to (FluidContainer(mutableMapOf(
FluidTank(INPUT_TANK_CAPACITY) to StorageDirection.STORE_ONLY,
FluidTank(OUTPUT_TANK_CAPACITY) to StorageDirection.EMPTY_ONLY
)) to mutableListOf(null, Direction.UP))
)
) to mutableListOf(null, Direction.UP))
), MenuProvider {
companion object {
const val INPUT_TANK_CAPACITY = 8000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ class GeneratorBlockEntity(
ModBlockEntityTypes.GENERATOR.get(),
pPos,
pBlockState,
ForgeCapabilities.ENERGY to (EnergyBattery(50000, 0, 2000) to mutableListOf(Direction.WEST, null)),
ForgeCapabilities.ITEM_HANDLER to (IndexableItemHandler(mutableListOf(64 to StorageDirection.STORE_ONLY)) to mutableListOf(Direction.UP, null))
ForgeCapabilities.ENERGY to
(EnergyBattery(50000, 0, 2000) to mutableListOf(Direction.WEST, null)),
ForgeCapabilities.ITEM_HANDLER to
(IndexableItemHandler(mutableListOf(64 to StorageDirection.STORE_ONLY)) to mutableListOf(Direction.UP, null))
) {
init {
capabilityHolder.capability<IndexableItemHandler>(ForgeCapabilities.ITEM_HANDLER).insertItemCheck = { _, stack, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package breadmod.block.machine.entity
import breadmod.recipe.fluidEnergy.ToasterRecipe
import breadmod.registry.block.ModBlockEntityTypes
import breadmod.registry.recipe.ModRecipeTypes
import breadmod.util.capability.CapabilityHolder.Companion.ACCEPT_ALL
import breadmod.util.capability.IndexableItemHandler
import breadmod.util.capability.StorageDirection
import net.minecraft.core.BlockPos
Expand All @@ -24,9 +25,11 @@ class ToasterBlockEntity(
pPos,
pBlockState,
ModRecipeTypes.TOASTING,
IndexableItemHandler(listOf(
2 to StorageDirection.STORE_ONLY
)) to null,
IndexableItemHandler(
listOf(
2 to StorageDirection.STORE_ONLY
)
) to ACCEPT_ALL,
listOf(0),
1 to 1
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import breadmod.block.machine.entity.menu.WheatCrusherMenu
import breadmod.recipe.fluidEnergy.WheatCrushingRecipe
import breadmod.registry.block.ModBlockEntityTypes
import breadmod.registry.recipe.ModRecipeTypes
import breadmod.util.capability.CapabilityHolder.Companion.ACCEPT_ALL
import breadmod.util.capability.EnergyBattery
import breadmod.util.capability.IndexableItemHandler
import breadmod.util.capability.StorageDirection
Expand All @@ -26,13 +27,15 @@ class WheatCrusherBlockEntity(
pPos,
pBlockState,
ModRecipeTypes.WHEAT_CRUSHING,
IndexableItemHandler(listOf(
64 to StorageDirection.STORE_ONLY,
64 to StorageDirection.EMPTY_ONLY
)) to null,
IndexableItemHandler(
listOf(
64 to StorageDirection.STORE_ONLY,
64 to StorageDirection.EMPTY_ONLY
)
) to ACCEPT_ALL,
listOf(0),
1 to 1,
EnergyBattery(50000, 2000) to null
EnergyBattery(50000, 2000) to ACCEPT_ALL
), MenuProvider {
override fun getDisplayName(): Component = modTranslatable("block", "wheat_crusher")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import breadmod.ModMain.modLocation
import breadmod.ModMain.modTranslatable
import breadmod.block.machine.entity.menu.DoughMachineMenu
import breadmod.network.PacketHandler.NETWORK
import breadmod.network.VoidTankPacket
import breadmod.network.server.VoidTankPacket
import breadmod.util.capability.EnergyBattery
import breadmod.util.capability.FluidContainer
import breadmod.util.formatUnit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package breadmod.block.machine.multiblock.generic.entity

import breadmod.block.machine.entity.AbstractMachineBlockEntity
import breadmod.registry.block.ModBlockEntityTypes
import breadmod.util.capability.CapabilityHolder.Companion.ACCEPT_ALL
import breadmod.util.capability.EnergyBattery
import net.minecraft.core.BlockPos
import net.minecraft.world.level.block.state.BlockState
import net.minecraftforge.common.capabilities.ForgeCapabilities

class PowerInterfaceBlockEntity(pPos: BlockPos, pBlockState: BlockState) : AbstractMachineBlockEntity<PowerInterfaceBlockEntity>(
ModBlockEntityTypes.MULTIBLOCK_GENERIC_POWER.get(), pPos, pBlockState,
ForgeCapabilities.ENERGY to (EnergyBattery(750000, 1000) to null)
ForgeCapabilities.ENERGY to (EnergyBattery(750000, 1000) to ACCEPT_ALL)
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package breadmod.block.storage.entity

import breadmod.block.util.ModBlockStateProperties
import breadmod.block.machine.entity.AbstractMachineBlockEntity
import breadmod.block.util.ModBlockStateProperties
import breadmod.registry.block.ModBlockEntityTypes
import breadmod.util.capability.CapabilityHolder.Companion.ACCEPT_ALL
import breadmod.util.capability.EnergyBattery
import net.minecraft.core.BlockPos
import net.minecraft.world.level.Level
Expand All @@ -17,7 +18,9 @@ class EnergyStorageBlockEntity(
ModBlockEntityTypes.ENERGY_STORAGE.get(),
pPos,
pBlockState,
ForgeCapabilities.ENERGY to (EnergyBattery(1000000, 50000, 50000) to null)
ForgeCapabilities.ENERGY to (EnergyBattery(
1000000, 50000, 50000
) to ACCEPT_ALL)
) {

override fun tick(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package breadmod.block.storage.entity

import breadmod.block.machine.entity.AbstractMachineBlockEntity
import breadmod.registry.block.ModBlockEntityTypes
import breadmod.util.capability.CapabilityHolder.Companion.ACCEPT_ALL
import breadmod.util.capability.FluidContainer
import breadmod.util.capability.StorageDirection
import net.minecraft.core.BlockPos
Expand All @@ -16,9 +17,11 @@ class FluidStorageBlockEntity(
ModBlockEntityTypes.FLUID_STORAGE.get(),
pPos,
pBlockState,
ForgeCapabilities.FLUID_HANDLER to (FluidContainer(mutableMapOf(
FluidTank(50000) to StorageDirection.BIDIRECTIONAL
)) to null)
ForgeCapabilities.FLUID_HANDLER to (FluidContainer(
mutableMapOf(
FluidTank(50000) to StorageDirection.BIDIRECTIONAL
)
) to ACCEPT_ALL)
) {
fun fluidHandler() = capabilityHolder.capabilityOrNull<FluidContainer>(ForgeCapabilities.FLUID_HANDLER)
}
25 changes: 21 additions & 4 deletions src/main/kotlin/breadmod/item/WrenchItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,32 @@ import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Rarity
import net.minecraft.world.item.context.UseOnContext
import net.minecraft.world.level.block.state.properties.BlockStateProperties
import net.minecraftforge.common.capabilities.ForgeCapabilities

class WrenchItem: Item(Properties().stacksTo(1).rarity(Rarity.UNCOMMON)) {
/**
* Wrench item, used for configuring the sides of machines, or toggling the [BlockStateProperties.ENABLED] state of machines.
*
* @see [AbstractMachineBlockEntity]
* @author Miko Elbrecht
* @since 1.0.0
*/
class WrenchItem : Item(Properties().stacksTo(1).rarity(Rarity.UNCOMMON)) {
/**
* Called when the player uses the item on a block; if this is on a [AbstractMachineBlockEntity], the wrench item will consume
* the current use.
*
* @author Miko Elbrecht
* @since 1.0.0
*/
override fun onItemUseFirst(stack: ItemStack, context: UseOnContext): InteractionResult {
val level = context.level
if(level.isClientSide) return InteractionResult.PASS
if (level.isClientSide) return InteractionResult.PASS
level.getBlockEntity(context.clickedPos)?.let {
if(it is AbstractMachineBlockEntity<*>) {
it.capabilityHolder.capabilities[ForgeCapabilities.ENERGY]?.second?.removeIf { it != null }
if (it is AbstractMachineBlockEntity<*>) {
it.capabilityHolder.getSidedness(ForgeCapabilities.ENERGY)


return InteractionResult.CONSUME
}
}
Expand Down
Loading

0 comments on commit e789193

Please sign in to comment.