Add filled matter capacitors in inventory tab
This commit is contained in:
parent
bbadfe8fb1
commit
13453511c6
@ -3,8 +3,10 @@ package ru.dbotthepony.mc.otm.item
|
|||||||
import net.minecraft.ChatFormatting
|
import net.minecraft.ChatFormatting
|
||||||
import net.minecraft.MethodsReturnNonnullByDefault
|
import net.minecraft.MethodsReturnNonnullByDefault
|
||||||
import net.minecraft.core.Direction
|
import net.minecraft.core.Direction
|
||||||
|
import net.minecraft.core.NonNullList
|
||||||
import net.minecraft.nbt.CompoundTag
|
import net.minecraft.nbt.CompoundTag
|
||||||
import net.minecraft.network.chat.Component
|
import net.minecraft.network.chat.Component
|
||||||
|
import net.minecraft.world.item.CreativeModeTab
|
||||||
import net.minecraft.world.item.Item
|
import net.minecraft.world.item.Item
|
||||||
import net.minecraft.world.item.ItemStack
|
import net.minecraft.world.item.ItemStack
|
||||||
import net.minecraft.world.item.Rarity
|
import net.minecraft.world.item.Rarity
|
||||||
@ -20,32 +22,29 @@ import ru.dbotthepony.mc.otm.capability.matter.*
|
|||||||
import ru.dbotthepony.mc.otm.core.ImpreciseFraction
|
import ru.dbotthepony.mc.otm.core.ImpreciseFraction
|
||||||
import ru.dbotthepony.mc.otm.core.formatMatter
|
import ru.dbotthepony.mc.otm.core.formatMatter
|
||||||
import ru.dbotthepony.mc.otm.core.ifPresentK
|
import ru.dbotthepony.mc.otm.core.ifPresentK
|
||||||
|
import ru.dbotthepony.mc.otm.core.tagNotNull
|
||||||
import javax.annotation.ParametersAreNonnullByDefault
|
import javax.annotation.ParametersAreNonnullByDefault
|
||||||
|
|
||||||
@ParametersAreNonnullByDefault
|
@ParametersAreNonnullByDefault
|
||||||
@MethodsReturnNonnullByDefault
|
@MethodsReturnNonnullByDefault
|
||||||
class MatterCapacitorItem : Item {
|
class MatterCapacitorItem : Item {
|
||||||
private inner class ItemMatterCapacitorCapability(private val stack: ItemStack) : ICapabilityProvider, IMatterHandler {
|
private inner class Matter(private val stack: ItemStack) : ICapabilityProvider, IMatterHandler {
|
||||||
private val resolver = LazyOptional.of<IMatterHandler> { this }
|
private val resolver = LazyOptional.of<IMatterHandler> { this }
|
||||||
|
|
||||||
override fun <T> getCapability(cap: Capability<T>, side: Direction?): LazyOptional<T> {
|
override fun <T> getCapability(cap: Capability<T>, side: Direction?): LazyOptional<T> {
|
||||||
return if (cap === MatteryCapability.MATTER) resolver.cast() else LazyOptional.empty()
|
return if (cap === MatteryCapability.MATTER) resolver.cast() else LazyOptional.empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun matter(): ImpreciseFraction {
|
override var storedMatter: ImpreciseFraction
|
||||||
val tag = stack.orCreateTag
|
get() {
|
||||||
return if (tag.contains("matter")) {
|
val tag = stack.orCreateTag
|
||||||
ImpreciseFraction.deserializeNBT(tag["matter"])
|
return if (tag.contains("matter")) {
|
||||||
} else ImpreciseFraction.ZERO
|
ImpreciseFraction.deserializeNBT(tag["matter"])
|
||||||
}
|
} else ImpreciseFraction.ZERO
|
||||||
|
}
|
||||||
private fun matter(value: ImpreciseFraction) {
|
set(value) {
|
||||||
stack.getOrCreateTag().put("matter", value.serializeNBT())
|
stack.tagNotNull.put("matter", value.serializeNBT())
|
||||||
}
|
}
|
||||||
|
|
||||||
override val storedMatter: ImpreciseFraction get() {
|
|
||||||
return if (isCreative) ImpreciseFraction.LONG_MAX_VALUE else matter()
|
|
||||||
}
|
|
||||||
|
|
||||||
override val maxStoredMatter: ImpreciseFraction get() {
|
override val maxStoredMatter: ImpreciseFraction get() {
|
||||||
return capacity
|
return capacity
|
||||||
@ -61,11 +60,11 @@ class MatterCapacitorItem : Item {
|
|||||||
|
|
||||||
override fun receiveMatterInner(howMuch: ImpreciseFraction, simulate: Boolean): ImpreciseFraction {
|
override fun receiveMatterInner(howMuch: ImpreciseFraction, simulate: Boolean): ImpreciseFraction {
|
||||||
if (isCreative) return howMuch
|
if (isCreative) return howMuch
|
||||||
val new = matter().plus(howMuch.coerceAtMost(maxInput)).coerceAtMost(capacity)
|
val new = storedMatter.plus(howMuch.coerceAtMost(maxInput)).coerceAtMost(capacity)
|
||||||
val diff = new.minus(matter())
|
val diff = new.minus(storedMatter)
|
||||||
|
|
||||||
if (!simulate) {
|
if (!simulate) {
|
||||||
matter(new)
|
storedMatter = new
|
||||||
}
|
}
|
||||||
|
|
||||||
return diff
|
return diff
|
||||||
@ -77,11 +76,11 @@ class MatterCapacitorItem : Item {
|
|||||||
|
|
||||||
override fun extractMatterInner(howMuch: ImpreciseFraction, simulate: Boolean): ImpreciseFraction {
|
override fun extractMatterInner(howMuch: ImpreciseFraction, simulate: Boolean): ImpreciseFraction {
|
||||||
if (isCreative) return howMuch
|
if (isCreative) return howMuch
|
||||||
val new = matter().minus(howMuch.coerceAtMost(maxOutput)).moreThanZero()
|
val new = storedMatter.minus(howMuch.coerceAtMost(maxOutput)).moreThanZero()
|
||||||
val diff = matter().minus(new)
|
val diff = storedMatter.minus(new)
|
||||||
|
|
||||||
if (!simulate) {
|
if (!simulate) {
|
||||||
matter(new)
|
storedMatter = new
|
||||||
}
|
}
|
||||||
|
|
||||||
return diff
|
return diff
|
||||||
@ -110,6 +109,20 @@ class MatterCapacitorItem : Item {
|
|||||||
_capacity = { ImpreciseFraction.LONG_MAX_VALUE }
|
_capacity = { ImpreciseFraction.LONG_MAX_VALUE }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun fillItemCategory(p_41391_: CreativeModeTab, p_41392_: NonNullList<ItemStack>) {
|
||||||
|
super.fillItemCategory(p_41391_, p_41392_)
|
||||||
|
|
||||||
|
if (!isCreative && allowedIn(p_41391_)) {
|
||||||
|
p_41392_.add(ItemStack(this).also {
|
||||||
|
it.getCapability(MatteryCapability.MATTER).ifPresentK {
|
||||||
|
if (it is Matter) {
|
||||||
|
it.storedMatter = it.maxStoredMatter
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun isBarVisible(p_150899_: ItemStack): Boolean {
|
override fun isBarVisible(p_150899_: ItemStack): Boolean {
|
||||||
if (isCreative)
|
if (isCreative)
|
||||||
return false
|
return false
|
||||||
@ -154,7 +167,7 @@ class MatterCapacitorItem : Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun initCapabilities(stack: ItemStack, nbt: CompoundTag?): ICapabilityProvider {
|
override fun initCapabilities(stack: ItemStack, nbt: CompoundTag?): ICapabilityProvider {
|
||||||
return ItemMatterCapacitorCapability(stack)
|
return Matter(stack)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
Loading…
Reference in New Issue
Block a user