Skip to content

Commit

Permalink
y,,
Browse files Browse the repository at this point in the history
  • Loading branch information
ATPStorages committed Jul 14, 2024
1 parent ec01b59 commit 15e5f90
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 99 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@
- fix up and polish existing modes
- tool gun displaying mode information and image accompanying said mode
# BUGS
- [ ] items inputted into machines from their sides are voided out of existence
- [X] items inputted into machines from their sides are voided out of existence
- [X] items not saving on world save/load
- [ ] (minor) tool gun anim speeds up when you have multiple tool guns at once
- [ ] Fluid texture on diesel machine needs to be normalized
- [ ] tool gun stacking up/absorbing clicks when not equipped
- [ ] (major) tool gun stacking up/absorbing clicks when not equipped
- [ ] happy block explosion not actually exploding an area
- [ ] (Production specific) Tool gun action not triggering
- [ ] (production specific) Tool gun action not triggering
- [ ] BMExplosion causing crash
---
- [X] Joke item: "the ultimate bread" just gives you creative mode lmao
Expand Down
1 change: 1 addition & 0 deletions advanced/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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
1 change: 1 addition & 0 deletions src/main/java/breadmod/mixin/MixinHumanoidArmorLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public MixinHumanoidArmorLayer(RenderLayerParent<T, M> pRenderer) {
private void renderArmorPiece(PoseStack pPoseStack, MultiBufferSource pBuffer, T pLivingEntity, EquipmentSlot pSlot, int pPackedLight, A pModel, CallbackInfo callbackInfo) {
ItemStack itemStack = pLivingEntity.getItemBySlot(pSlot);
Item item = itemStack.getItem();
// TODO: Better way to write this
if(item instanceof BreadArmorItem && ((BreadArmorItem) item).getEquipmentSlot() == pSlot) {
this.getParentModel().copyPropertiesTo(pModel);
this.iSetPartVisibility(pModel, pSlot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import net.minecraft.world.level.material.PushReaction
import net.minecraft.world.phys.shapes.CollisionContext
import net.minecraft.world.phys.shapes.VoxelShape

class HellNaw: ButtonBlock(
class HellNawButtonBlock: ButtonBlock(
Properties.of()
.noCollission()
.strength(0.5f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,17 @@ abstract class AbstractMachineBlockEntity<T: AbstractMachineBlockEntity<T>>(
): BlockEntity(pType, pPos, pBlockState) {
val capabilityHolder = CapabilityHolder(mapOf(*additionalCapabilities))

init {
this.capabilityHolder.capabilities.forEach { (_, u) ->
u.first.ifPresent { it.changed = { println("saved"); setChanged() } }
}
}

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)*/ Direction.NORTH, side) ?: super.getCapability(cap, side)
= capabilityHolder.capabilitySided(cap, blockState.getValue(BlockStateProperties.HORIZONTAL_FACING), side)
?: super.getCapability(cap, side)
final override fun invalidateCaps() {
capabilityHolder.invalidate()
super.invalidateCaps()
Expand All @@ -51,23 +58,37 @@ abstract class AbstractMachineBlockEntity<T: AbstractMachineBlockEntity<T>>(

open fun adjustSaveAdditional(pTag: CompoundTag) {}
final override fun saveAdditional(pTag: CompoundTag) {
adjustSaveAdditional(pTag)
val additionalCompound = CompoundTag()
adjustSaveAdditional(additionalCompound)
val breadmodCompound = CompoundTag()
capabilityHolder.capabilities.forEach { (t, u) ->
u.first.ifPresent { breadmodCompound.put(t.name, it.serializeNBT()) }
}

pTag.put(ADDITIONAL_KEY, additionalCompound)
pTag.put(BREADMOD_CAPABILITY_KEY, breadmodCompound)
super.saveAdditional(pTag)
}

open fun adjustLoad(pTag: CompoundTag) {}
final override fun load(pTag: CompoundTag) {
adjustLoad(pTag)
adjustLoad(pTag.getCompound(ADDITIONAL_KEY))
val capabilityData = pTag.getCompound(BREADMOD_CAPABILITY_KEY)
capabilityHolder.capabilities.forEach { (t, u) ->
u.first.ifPresent { it.deserializeNBT(capabilityData.get(t.name)) }
}
super.load(pTag)
}

open fun adjustChanged() {}
final override fun setChanged() = super.setChanged().also {
final override fun setChanged() {
adjustChanged()
if(level is ServerLevel) NETWORK.send(
PacketDistributor.TRACKING_CHUNK.with { (level as ServerLevel).getChunkAt(blockPos) },
CapabilityDataPacket(blockPos, updateTag)
)
super.setChanged()
}

init {
Expand Down Expand Up @@ -197,4 +218,9 @@ abstract class AbstractMachineBlockEntity<T: AbstractMachineBlockEntity<T>>(
//const val RECIPE_KEY = "currentRecipe"
}
}

private companion object {
const val ADDITIONAL_KEY = "additional"
const val BREADMOD_CAPABILITY_KEY = "capability"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import net.minecraft.world.entity.player.StackedContents
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.ForgeCapabilities
import net.minecraftforge.fluids.capability.templates.FluidTank
import kotlin.math.min

class GeneratorBlockEntity(
pPos: BlockPos,
Expand Down Expand Up @@ -47,37 +47,18 @@ class GeneratorBlockEntity(
return true
}

private fun runSignsOne(lambda: (Int) -> Unit) {
var i = -1
while (i < 2) {
lambda(i)
i += 2
}
}

override fun postTick(
override fun recipeTick(
pLevel: Level,
pPos: BlockPos,
pState: BlockState,
pBlockEntity: AbstractMachineBlockEntity<GeneratorBlockEntity>
pBlockEntity: Progressive<GeneratorBlockEntity, GeneratorRecipe>,
recipe: GeneratorRecipe
) {
val energyHandle = capabilityHolder.capability<EnergyBattery>(ForgeCapabilities.ENERGY)
val blockEntities = buildList {
runSignsOne { x -> pLevel.getBlockEntity(pPos.offset(x, 0, 0))?.getCapability(ForgeCapabilities.ENERGY)?.let { add(it) } }
runSignsOne { y -> pLevel.getBlockEntity(pPos.offset(0, y, 0))?.getCapability(ForgeCapabilities.ENERGY)?.let { add(it) } }
runSignsOne { z -> pLevel.getBlockEntity(pPos.offset(0, 0, z))?.getCapability(ForgeCapabilities.ENERGY)?.let { add(it) } }
}

if(blockEntities.isNotEmpty()) {
val total = min(energyHandle.bMaxExtract, energyHandle.stored)
val toDistribute = total / blockEntities.size
var distributed = 0
blockEntities.forEach { opt -> opt.ifPresent {
distributed += it.receiveEnergy(toDistribute, true)
if(distributed > total) { energyHandle.extractEnergy(total, false); return@ifPresent }
} }
energyHandle.extractEnergy(distributed, false)
}
val sides = (capabilityHolder.capabilities[ForgeCapabilities.ENERGY] ?: return).second
capabilityHolder.capability<EnergyBattery>(ForgeCapabilities.ENERGY).distribute(
pLevel, pPos, sides,
blockState.getValue(BlockStateProperties.HORIZONTAL_FACING)
)
}

override fun clearContent() { getItemHandler()?.clear() }
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/breadmod/registry/block/ModBlocks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ object ModBlocks {
val HELL_NAW = deferredRegister.registerBlockItem(
ModItems.deferredRegister,
"hell_naw",
{ HellNaw() },
{ HellNawButtonBlock() },
Item.Properties()
)

Expand Down
13 changes: 12 additions & 1 deletion src/main/kotlin/breadmod/util/General.kt
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,15 @@ fun DeferredRegister<Block>.registerBlockItem(itemRegister: DeferredRegister<Ite
this.register(id, block).let { itemRegister.register(id) { item(it.get()) } }

fun <T: Recipe<*>> DeferredRegister<RecipeType<*>>.registerType(name: String): RegistryObject<RecipeType<T>> =
this.register(name) { object : RecipeType<T> { override fun toString(): String = name } }
this.register(name) { object : RecipeType<T> { override fun toString(): String = name } }

fun translateDirection(translateFor: Direction, side: Direction?): Direction? =
if(side == null) null
else if(side.axis == Direction.Axis.Y) side
else when(translateFor) {
Direction.NORTH -> side.opposite
Direction.SOUTH -> side
Direction.EAST -> side.clockWise
Direction.WEST -> side.counterClockWise
else -> null
}
6 changes: 3 additions & 3 deletions src/main/kotlin/breadmod/util/capability/CapabilityHolder.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package breadmod.util.capability

import breadmod.util.translateDirection
import net.minecraft.core.Direction
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.Tag
Expand Down Expand Up @@ -54,11 +55,10 @@ class CapabilityHolder(passedCapabilities: Map<Capability<*>, CapabilityContaine
* @see capability
* @see capabilityOrNull
*/

// TODO TRANSLATEFOR
fun <T> capabilitySided(capability: Capability<T>, translateFor: Direction, side: Direction?): LazyOptional<T>? {
val translatedSide = translateDirection(translateFor, side)
capabilities.forEach { (myCapability, pair) ->
if(myCapability == capability && (pair.second == null || (pair.second ?: return@forEach).contains(side))) return pair.first.cast()
if(myCapability == capability && (pair.second == null || (pair.second ?: return@forEach).contains(translatedSide))) return pair.first.cast()
}
return null
}
Expand Down
28 changes: 26 additions & 2 deletions src/main/kotlin/breadmod/util/capability/EnergyBattery.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package breadmod.util.capability

import breadmod.util.translateDirection
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.nbt.CompoundTag
import net.minecraft.world.level.Level
import net.minecraftforge.common.capabilities.ForgeCapabilities
import net.minecraftforge.energy.IEnergyStorage
import kotlin.jvm.optionals.getOrNull
import kotlin.math.min

class EnergyBattery(
capacity: Int, var bMaxReceive: Int = Int.MAX_VALUE, var bMaxExtract: Int = Int.MAX_VALUE,
capacity: Int,
private var bMaxReceive: Int = Int.MAX_VALUE, var bMaxExtract: Int = Int.MAX_VALUE,
override var changed: (() -> Unit)? = null
) : IEnergyStorage, ICapabilitySavable<CompoundTag> {
constructor(capacity: Int, bTransportLimit: Int = Int.MAX_VALUE, changed: (() -> Unit)? = null): this(capacity, bTransportLimit, bTransportLimit, changed)

var stored: Int = 0
var capacity: Int = capacity
private var capacity: Int = capacity
set(value) { stored = min(stored, value); field = value }

override fun receiveEnergy(maxReceive: Int, simulate: Boolean): Int = if(bMaxReceive > 0) {
Expand Down Expand Up @@ -56,6 +63,23 @@ class EnergyBattery(
}
}

fun distribute(pLevel: Level, pPos: BlockPos, sides: List<Direction?>?, facing: Direction = Direction.NORTH) {
val energies = (sides?.filterNotNull() ?: Direction.entries).mapNotNull {
val rotated = translateDirection(facing, it)!!.let { r -> if(r.axis == Direction.Axis.Z) r.opposite else r }
pLevel.getBlockEntity(pPos.offset(rotated.normal))?.getCapability(
ForgeCapabilities.ENERGY,
rotated.opposite
)?.resolve()?.getOrNull()
}

if (energies.isNotEmpty()) {
val toDistribute = this.extractEnergy(bMaxExtract, true) / energies.size
var distributed = 0
energies.forEach { distributed += it.receiveEnergy(toDistribute, false) }
this.extractEnergy(distributed, false)
}
}

internal companion object {
const val STORED_TAG = "stored"
const val CAPACITY_TAG = "capacity"
Expand Down
70 changes: 13 additions & 57 deletions src/main/kotlin/breadmod/util/capability/IndexableItemHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,18 @@ open class IndexableItemHandler(private val slots: List<Pair<Int, StorageDirecti

override fun insertItem(slot: Int, stack: ItemStack, simulate: Boolean): ItemStack = stack.copy().also {
val reifiedSlot = slots[slot]
if(reifiedSlot.second != StorageDirection.EMPTY_ONLY) {
val space = min(min(stack.maxStackSize, reifiedSlot.first)-stack.count, it.count)
val reifiedStack = stacks[slot]
val toMove = min(min(reifiedSlot.first, reifiedStack.maxStackSize) - reifiedStack.count, it.count)

if(reifiedSlot.second != StorageDirection.EMPTY_ONLY
&& toMove > 0
&& (reifiedStack.isEmpty || stack.item == reifiedStack.item)
) {
if(!simulate) {
if(stack.isEmpty) stacks[slot] = it.copyWithCount(space)
else stack.grow(space)
changed?.invoke()
if(reifiedStack.isEmpty) stacks[slot] = it.copyWithCount(toMove)
else stacks[slot].grow(toMove)
}
it.shrink(space)
if(it.count == 0) return ItemStack.EMPTY
it.shrink(toMove)
} else return it
}

Expand All @@ -104,38 +107,9 @@ open class IndexableItemHandler(private val slots: List<Pair<Int, StorageDirecti
* @since 1.0.0
*/
@Suppress("MemberVisibilityCanBePrivate", "unused")
fun insertItem(stack: ItemStack, simulate: Boolean): ItemStack = stack.copy().also {
val toFill = slots
.filterIndexed { index, (max, direction) -> max > 0 && stacks[index].let { actual -> actual.isEmpty ||( actual.`is`(it.item) && actual.count != actual.maxStackSize) } && direction != StorageDirection.EMPTY_ONLY }
.mapIndexed { index, pair -> stacks[index] to (pair to index) }
.sortedWith { (stack1, pair1), (stack2, pair2) ->
val cmp = (stack1.maxStackSize-stack1.count).compareTo(stack2.maxStackSize-stack2.count)
if(cmp != 1) pair1.first.first.compareTo(pair2.first.first) else cmp
}
toFill.forEach { (stack, pair) ->
val space = min(min(stack.maxStackSize, pair.first.first)-stack.count, it.count)
if(!simulate) {
if(stack.isEmpty) stacks[pair.second] = it.copyWithCount(space)
else stack.grow(space)
}

it.shrink(space)
if(it.count == 0) return ItemStack.EMPTY
}
fun insertItem(stack: ItemStack, simulate: Boolean): ItemStack = TODO("insert items 2")

if(!simulate && it.count != stack.count) changed?.invoke()
return it
}

override fun extractItem(slot: Int, amount: Int, simulate: Boolean): ItemStack {
val reifiedSlot = slots[slot]
if(reifiedSlot.second != StorageDirection.STORE_ONLY) {
val stack = stacks[slot]
val removed = stack.copyWithCount(min(amount, stack.count))
if(!simulate) stack.shrink(removed.count).also { changed?.invoke() }
return removed
} else return ItemStack.EMPTY
}
override fun extractItem(slot: Int, amount: Int, simulate: Boolean): ItemStack = TODO("extr items")

/**
* Non-specific slot version of [extractItem].
Expand All @@ -144,25 +118,7 @@ open class IndexableItemHandler(private val slots: List<Pair<Int, StorageDirecti
* @since 1.0.0
*/
@Suppress("MemberVisibilityCanBePrivate", "unused", "ReplaceNotNullAssertionWithElvisReturn")
fun extractItem(target: Item? = null, amount: Int, simulate: Boolean): ItemStack {
val reifiedTarget = target ?: (stacks.firstOrNull { !it.isEmpty } ?: return ItemStack.EMPTY).item
var remainder = amount

val toFill = slots
.filterIndexed { index, (max, direction) -> max > 0 && stacks[index].let { actual -> !actual.isEmpty && actual.`is`(reifiedTarget) } && direction != StorageDirection.STORE_ONLY }
.mapIndexed { index, pair -> stacks[index] to pair }
.sortedWith { (stack1), (stack2) -> stack1.count.compareTo(stack2.count) }
var concatStack: ItemStack? = null
toFill.forEach { (stack) ->
val toRemove = min(remainder, stack.count)
if(concatStack == null) concatStack = stack.copyWithCount(toRemove) else concatStack!!.grow(toRemove)
remainder -= concatStack!!.count
if(!simulate) stack.shrink(toRemove)
}

if(!simulate) changed?.invoke()
return concatStack ?: ItemStack.EMPTY
}
fun extractItem(target: Item? = null, amount: Int, simulate: Boolean): ItemStack = TODO("extr items 2")

override fun getSlotLimit(slot: Int): Int {
val slotMax = slots[slot].first
Expand Down

0 comments on commit 15e5f90

Please sign in to comment.