-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
71 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 46 additions & 28 deletions
74
src/main/kotlin/juuxel/adorn/block/entity/BaseInventoryBlockEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,75 @@ | ||
package juuxel.adorn.block.entity | ||
|
||
import alexiil.mc.lib.attributes.AttributeList | ||
import alexiil.mc.lib.attributes.AttributeProvider | ||
import alexiil.mc.lib.attributes.item.impl.SimpleFixedItemInv | ||
import io.github.juuxel.polyester.registry.PolyesterBlock | ||
import juuxel.adorn.util.SidedFixedInventoryWrapper | ||
import juuxel.adorn.util.SidedInventoryImpl | ||
import net.minecraft.block.BlockState | ||
import net.minecraft.block.InventoryProvider | ||
import net.minecraft.block.entity.BlockEntity | ||
import net.minecraft.block.entity.BlockEntityType | ||
import net.minecraft.entity.player.PlayerEntity | ||
import net.minecraft.inventory.Inventories | ||
import net.minecraft.inventory.Inventory | ||
import net.minecraft.inventory.SidedInventory | ||
import net.minecraft.item.ItemStack | ||
import net.minecraft.nbt.CompoundTag | ||
import net.minecraft.util.DefaultedList | ||
import net.minecraft.util.math.BlockPos | ||
import net.minecraft.world.IWorld | ||
import net.minecraft.world.World | ||
|
||
abstract class BaseInventoryBlockEntity( | ||
type: BlockEntityType<*>, | ||
invSize: Int | ||
) : BlockEntity(type), AttributeProvider/*, InventoryProvider*/ { | ||
val inventory = SimpleFixedItemInv(invSize) | ||
|
||
init { | ||
inventory.addListener({ _, _, _, _ -> markDirty() }, {}) | ||
} | ||
private val invSize: Int | ||
) : BlockEntity(type), Inventory { | ||
val items: DefaultedList<ItemStack> = DefaultedList.create(invSize, ItemStack.EMPTY) | ||
val sidedInventory: SidedInventory = SidedInventoryImpl(this) | ||
|
||
override fun toTag(tag: CompoundTag) = super.toTag(tag).apply { | ||
put("Items", inventory.toTag()) | ||
Inventories.toTag(tag, items) | ||
} | ||
|
||
override fun fromTag(tag: CompoundTag) { | ||
super.fromTag(tag) | ||
inventory.fromTag(tag.getCompound("Items")) | ||
Inventories.fromTag(tag, items) | ||
} | ||
|
||
override fun addAllAttributes(world: World, pos: BlockPos, state: BlockState, attributes: AttributeList<*>) { | ||
attributes.offer(inventory) | ||
attributes.offer(inventory.extractable) | ||
attributes.offer(inventory.insertable) | ||
override fun getInvStack(slot: Int) = items[slot] | ||
|
||
override fun clear() { | ||
items.clear() | ||
markDirty() | ||
} | ||
|
||
// override fun getInventory(state: BlockState, world: IWorld, pos: BlockPos) = | ||
// SidedFixedInventoryWrapper(inventory) | ||
override fun setInvStack(slot: Int, stack: ItemStack) { | ||
items[slot] = stack | ||
} | ||
|
||
override fun removeInvStack(slot: Int) = | ||
Inventories.removeStack(items, slot) | ||
|
||
override fun canPlayerUseInv(player: PlayerEntity?) = true | ||
|
||
override fun getInvSize() = invSize | ||
|
||
// Uses PolyesterBlock to force being applied to a block | ||
interface BlockAttributeProviderImpl : PolyesterBlock, AttributeProvider, InventoryProvider { | ||
override fun addAllAttributes(world: World, pos: BlockPos, state: BlockState, attributes: AttributeList<*>) { | ||
(world.getBlockEntity(pos) as? BaseInventoryBlockEntity) | ||
?.addAllAttributes(world, pos, state, attributes) | ||
override fun takeInvStack(p0: Int, p1: Int) = | ||
Inventories.splitStack(items, p0, p1).also { | ||
if (!it.isEmpty) { | ||
markDirty() | ||
} | ||
} | ||
|
||
override fun isInvEmpty(): Boolean { | ||
for (stack in items) { | ||
if (!stack.isEmpty) { | ||
return false | ||
} | ||
} | ||
|
||
override fun getInventory(state: BlockState, world: IWorld, pos: BlockPos) = | ||
return true | ||
} | ||
|
||
interface InventoryProviderImpl : InventoryProvider { | ||
override fun getInventory(state: BlockState?, world: IWorld, pos: BlockPos): SidedInventory? = | ||
(world.getBlockEntity(pos) as? BaseInventoryBlockEntity)?.let { | ||
SidedFixedInventoryWrapper(it.inventory) | ||
it.sidedInventory | ||
} | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
src/main/kotlin/juuxel/adorn/util/SidedFixedInventoryWrapper.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package juuxel.adorn.util | ||
|
||
import net.minecraft.entity.player.PlayerEntity | ||
import net.minecraft.inventory.Inventory | ||
import net.minecraft.inventory.SidedInventory | ||
import net.minecraft.item.ItemStack | ||
import net.minecraft.util.math.Direction | ||
|
||
class SidedInventoryImpl(private val inv: Inventory) : Inventory by inv, SidedInventory { | ||
private val slots: IntArray = IntArray(inv.invSize) { it } | ||
|
||
override fun canPlayerUseInv(player: PlayerEntity?) = true | ||
|
||
override fun getInvAvailableSlots(var1: Direction?) = slots | ||
|
||
override fun canExtractInvStack(slot: Int, stack: ItemStack, direction: Direction?) = true | ||
|
||
override fun canInsertInvStack(slot: Int, stack: ItemStack, direction: Direction?) = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters