Skip to content

Commit

Permalink
hacky workaround for sided inv wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ttttdoy committed Jul 13, 2024
1 parent a5181d6 commit f41d68b
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract class AbstractMachineBlockEntity<T: AbstractMachineBlockEntity<T>>(

final override fun <T : Any?> getCapability(cap: Capability<T>): LazyOptional<T> =
getCapability(cap, null)
final override fun <T : Any?> getCapability(cap: Capability<T>, side: Direction?): LazyOptional<T>
override fun <T : Any?> getCapability(cap: Capability<T>, side: Direction?): LazyOptional<T>
= capabilityHolder.capabilitySided(cap, /*blockState.getValue(BlockStateProperties.HORIZONTAL_FACING)*/ Direction.NORTH, side) ?: super.getCapability(cap, side)
final override fun invalidateCaps() {
capabilityHolder.invalidate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ import breadmod.util.capability.EnergyBattery
import breadmod.util.capability.IndexableItemHandler
import breadmod.util.capability.StorageDirection
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.nbt.CompoundTag
import net.minecraft.network.chat.Component
import net.minecraft.world.MenuProvider
import net.minecraft.world.WorldlyContainer
import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.entity.player.Player
import net.minecraft.world.entity.player.StackedContents
import net.minecraft.world.inventory.AbstractContainerMenu
import net.minecraft.world.inventory.CraftingContainer
import net.minecraft.world.item.ItemStack
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.block.state.properties.BlockStateProperties
import net.minecraftforge.common.capabilities.Capability
import net.minecraftforge.common.capabilities.ForgeCapabilities
import net.minecraftforge.common.util.LazyOptional
import net.minecraftforge.items.IItemHandlerModifiable
import net.minecraftforge.items.wrapper.SidedInvWrapper

class WheatCrusherBlockEntity(
pPos: BlockPos,
Expand All @@ -35,8 +41,35 @@ class WheatCrusherBlockEntity(
ForgeCapabilities.ITEM_HANDLER to (IndexableItemHandler(listOf(
64 to StorageDirection.STORE_ONLY,
64 to StorageDirection.EMPTY_ONLY
)) to null)
), MenuProvider, CraftingContainer {
)) to mutableListOf())
), MenuProvider, WorldlyContainer {

// todo this needs to be adapted into CapabilityHolder or IndexableItemHandler, i'm not sure
/// Temporary workaround for CapabilityHolder not currently supporting proper sided inv wrapping for IndexableItemHandler
private val facing = blockState.getValue(BlockStateProperties.HORIZONTAL_FACING) /*?: blockState.getValue(BlockStateProperties.FACING) ?: null*/
private var handlers: Array<out LazyOptional<IItemHandlerModifiable>> = SidedInvWrapper.create(this, facing)

// Overriding AbstractMachineBlockEntity#getCapability to allow casting the handlers variable above
override fun <T> getCapability(cap: Capability<T>, side: Direction?): LazyOptional<T> {
return when {
(cap == ForgeCapabilities.ITEM_HANDLER) -> handlers[0].cast()
else -> capabilityHolder.capabilitySided(cap, /*blockState.getValue(BlockStateProperties.HORIZONTAL_FACING)*/ Direction.NORTH, side) ?: super.getCapability(cap, side)
}
}

// Binds slots 0 and 1 to all sides of the block
override fun getSlotsForFace(pSide: Direction): IntArray = when(pSide) {
Direction.UP, Direction.DOWN, Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST -> intArrayOf(0, 1)
else -> intArrayOf()
}

override fun canPlaceItemThroughFace(pIndex: Int, pItemStack: ItemStack, pDirection: Direction?): Boolean =
if(pDirection != null) getSlotsForFace(pDirection).contains(pIndex) else true

// Allow all but the input slot to be extracted from all faces
override fun canTakeItemThroughFace(pIndex: Int, pStack: ItemStack, pDirection: Direction): Boolean = pIndex != 0
///

override fun adjustSaveAdditionalProgressive(pTag: CompoundTag) {
super.adjustSaveAdditionalProgressive(pTag)
pTag.put(ModMain.ID, CompoundTag().also { dataTag ->
Expand All @@ -54,18 +87,6 @@ class WheatCrusherBlockEntity(
capabilityHolder.capabilityOrNull<EnergyBattery>(ForgeCapabilities.ENERGY)?.deserializeNBT(dataTag.getCompound("energy"))
}

override fun recipeTick(
pLevel: Level,
pPos: BlockPos,
pState: BlockState,
pBlockEntity: Progressive<WheatCrusherBlockEntity, WheatCrushingRecipe>,
recipe: WheatCrushingRecipe
) {
// println("serverside recipe progress: ${progress}t / ${progress / 20}s")
// println("serverside recipe time: ${currentRecipe.getOrNull()?.time}t / ${currentRecipe.getOrNull()?.time?.div(20)}s")
}


override fun createMenu(pContainerId: Int, pInventory: Inventory, pPlayer: Player): AbstractContainerMenu =
WheatCrusherMenu(pContainerId, pInventory, this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ abstract class AbstractMachineMenu<T : AbstractMachineBlockEntity.Progressive<T,
repeat(3) { y -> repeat(9) { x -> addSlot(Slot(inventory, x + y * 9 + 9, 8 + x * 18, inventoryY + y * 18)) } }
}

open val containerSlotCount: Int = 1 // number of inventory slots the block entity has
/**
* ### Used in [quickMoveStack] to enable shift clicking items into the target inventory
* value must match the amount of slots your block entity has
*/
open val containerSlotCount: Int = 1

final override fun quickMoveStack(playerIn: Player, pIndex: Int): ItemStack {
val sourceSlot = slots[pIndex]
if (!sourceSlot.hasItem()) return ItemStack.EMPTY //EMPTY_ITEM
Expand All @@ -47,7 +52,7 @@ abstract class AbstractMachineMenu<T : AbstractMachineBlockEntity.Progressive<T,
// This is a vanilla container slot so merge the stack into the tile inventory
if (!moveItemStackTo(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX + containerSlotCount, false)) return ItemStack.EMPTY // EMPTY_ITEM
} else if (pIndex < TE_INVENTORY_FIRST_SLOT_INDEX + containerSlotCount) {
// This is a TE slot so merge the stack into the players inventory
// This is a BE slot so merge the stack into the players inventory
if (!moveItemStackTo(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) return ItemStack.EMPTY
} else {
println("Invalid slotIndex:$pIndex")
Expand All @@ -74,7 +79,7 @@ abstract class AbstractMachineMenu<T : AbstractMachineBlockEntity.Progressive<T,
// Each time we add a Slot to the container, it automatically increases the slotIndex, which means
// 0 - 8 = hotbar slots (which will map to the InventoryPlayer slot numbers 0 - 8)
// 9 - 35 = player inventory slots (which map to the InventoryPlayer slot numbers 9 - 35)
// 36 - 44 = TileInventory slots, which map to our TileEntity slot numbers 0 - 8)
// 36 - 44 = TileInventory slots, which map to our BlockEntity slot numbers 0 - 8)
const val HOTBAR_SLOT_COUNT = 9
const val PLAYER_INVENTORY_ROW_COUNT = 3
const val PLAYER_INVENTORY_COLUMN_COUNT = 9
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/breadmod/datagen/ModItemModelProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class ModItemModelProvider(
singleItem(ModItems.CAPRISPIN)
singleItem(ModItems.TOAST)
handheldItem(ModItems.BREAD_GUN_ITEM)
singleItem(ModItems.TOASTER_HEATING_ELEMENT)

fenceInventory("bread_fence", modLoc("${ModelProvider.BLOCK_FOLDER}/bread_block"))
ModItems.PROJECT_E?.also {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class USEnglishLanguageProvider(
add(ModItems.CAPRISPIN)
add(ModItems.TOAST)
add(ModBlocks.TOASTER)
add(ModItems.TOASTER_HEATING_ELEMENT)

add(ModCreativeTabs.MAIN_TAB, "Bread Mod")
add(ModCreativeTabs.SPECIALS_TAB, "Bread Mod: Specials")
Expand Down
16 changes: 12 additions & 4 deletions src/main/kotlin/breadmod/registry/item/ModItems.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,26 @@ object ModItems {
val BREAD_BULLET_ITEM: RegistryObject<Item> = deferredRegister.register("bread_bullet") { Item(Item.Properties()) }
val TOOL_GUN: RegistryObject<Item> = deferredRegister.register(TOOL_GUN_DEF) { ToolGunItem() }
val CAPRISPIN: RegistryObject<Item> = deferredRegister.register("caprispin") { object : Item(Properties()
.food(FoodProperties.Builder().alwaysEat().nutrition(20).effect(MobEffectInstance(MobEffects.LEVITATION, 100, 20), 1f).build())
.rarity(Rarity.EPIC)){
.food(FoodProperties.Builder()
.alwaysEat()
.nutrition(20)
.effect(MobEffectInstance(MobEffects.LEVITATION, 100, 20), 1f)
.build()
)
.rarity(Rarity.EPIC)
){
override fun getUseAnimation(pStack: ItemStack): UseAnim = UseAnim.DRINK
} }
}}
val TOASTER_HEATING_ELEMENT: RegistryObject<Item> = deferredRegister.register("toaster_heating_element") { Item(Item.Properties()) }

val CREATURE: RegistryObject<Item> = deferredRegister.register("creature") { Item(Item.Properties()) }

val TEST_DISC: RegistryObject<RecordItem> = deferredRegister.register("music_disc_test") {
RecordItem(15, ModSounds.TEST_SOUND, Item.Properties()
.stacksTo(1)
.rarity(Rarity.RARE),
7900)
7900
)
}

val PROJECT_E: ProjectEItems? = if(ModList.get().isLoaded("projecte")) ProjectEItems() else null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"credit": "Made with Blockbench",
"parent": "block/block",
"texture_size": [32, 32],
"textures": {
"0": "breadmod:block/keyboard_assembly",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f41d68b

Please sign in to comment.