Some fixes for holo signs save/load, fix for redstone control

This commit is contained in:
DBotThePony 2023-02-23 15:38:43 +07:00
parent a9b28a66ca
commit 689d9cd5c1
Signed by: DBot
GPG Key ID: DCC23B5715498507
7 changed files with 54 additions and 33 deletions

View File

@ -136,7 +136,7 @@ fun addLootTables(lootTables: LootTables) {
lootTables.tile(MBlocks.ENERGY_SERVO) lootTables.tile(MBlocks.ENERGY_SERVO)
lootTables.tile(MBlocks.ENERGY_COUNTER) lootTables.tile(MBlocks.ENERGY_COUNTER)
lootTables.tile(MBlocks.CHEMICAL_GENERATOR) lootTables.tile(MBlocks.CHEMICAL_GENERATOR)
lootTables.tile(MBlocks.HOLO_SIGN) lootTables.tile(MBlocks.HOLO_SIGN, "isLocked")
lootTables.tile(MBlocks.STORAGE_CABLE) lootTables.tile(MBlocks.STORAGE_CABLE)
lootTables.tile(MBlocks.ANDROID_STATION) lootTables.tile(MBlocks.ANDROID_STATION)
lootTables.tile(MBlocks.BATTERY_BANK) lootTables.tile(MBlocks.BATTERY_BANK)

View File

@ -11,6 +11,7 @@ import net.minecraft.world.inventory.AbstractContainerMenu
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.ItemStack import net.minecraft.world.item.ItemStack
import net.minecraft.world.level.Level
import net.minecraftforge.common.capabilities.ForgeCapabilities import net.minecraftforge.common.capabilities.ForgeCapabilities
import net.minecraftforge.items.IItemHandler import net.minecraftforge.items.IItemHandler
import ru.dbotthepony.mc.otm.capability.CombinedItemHandler import ru.dbotthepony.mc.otm.capability.CombinedItemHandler
@ -30,6 +31,7 @@ import ru.dbotthepony.mc.otm.core.nbt.getJson
import ru.dbotthepony.mc.otm.core.nbt.putJson import ru.dbotthepony.mc.otm.core.nbt.putJson
import ru.dbotthepony.mc.otm.core.nbt.set import ru.dbotthepony.mc.otm.core.nbt.set
import ru.dbotthepony.mc.otm.core.util.ITickable import ru.dbotthepony.mc.otm.core.util.ITickable
import ru.dbotthepony.mc.otm.once
/** /**
* Device block entity base, implementing [MenuProvider] and [IRedstoneControlled], and also tracks custom display name * Device block entity base, implementing [MenuProvider] and [IRedstoneControlled], and also tracks custom display name
@ -38,13 +40,17 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
: MatteryBlockEntity(blockEntityType, blockPos, blockState), MenuProvider, IRedstoneControlled { : MatteryBlockEntity(blockEntityType, blockPos, blockState), MenuProvider, IRedstoneControlled {
var customDisplayName: Component? = null var customDisplayName: Component? = null
override val redstoneControl: AbstractRedstoneControl = RedstoneControl { new, old -> override val redstoneControl = RedstoneControl { new, old ->
setChangedLight() setChangedLight()
if (new != old) if (new != old)
redstoneStatusUpdated(new, old) redstoneStatusUpdated(new, old)
} }
init {
savetables.stateful(::redstoneControl, REDSTONE_CONTROL_KEY)
}
protected open val defaultDisplayName: Component protected open val defaultDisplayName: Component
get() = level?.getBlockState(blockPos)?.block?.name ?: TextComponent("null at $blockPos") get() = level?.getBlockState(blockPos)?.block?.name ?: TextComponent("null at $blockPos")
@ -58,18 +64,23 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
override fun saveAdditional(nbt: CompoundTag) { override fun saveAdditional(nbt: CompoundTag) {
super.saveAdditional(nbt) super.saveAdditional(nbt)
if (customDisplayName != null) if (customDisplayName != null)
nbt.putJson("Name", Component.Serializer.toJsonTree(customDisplayName!!)) nbt.putJson("Name", Component.Serializer.toJsonTree(customDisplayName!!))
nbt[REDSTONE_CONTROL_KEY] = redstoneControl.serializeNBT()
} }
override fun load(nbt: CompoundTag) { override fun load(nbt: CompoundTag) {
super.load(nbt) super.load(nbt)
customDisplayName = nbt.getJson("Name")?.let(Component.Serializer::fromJson) customDisplayName = nbt.getJson("Name")?.let(Component.Serializer::fromJson)
redstoneControl.deserializeNBT(nbt[REDSTONE_CONTROL_KEY] as? CompoundTag) }
override fun setLevel(level: Level) {
super.setLevel(level)
level.once {
if (!isRemoved && this.level == level) {
redstoneControl.redstoneSignal = level.getBestNeighborSignal(blockPos)
}
}
} }
inner class ConfigurableEnergy<T : IMatteryEnergyStorage>( inner class ConfigurableEnergy<T : IMatteryEnergyStorage>(

View File

@ -10,7 +10,7 @@ interface IRedstoneControlled {
val redstoneControl: AbstractRedstoneControl val redstoneControl: AbstractRedstoneControl
} }
abstract class AbstractRedstoneControl : INBTSerializable<CompoundTag> { abstract class AbstractRedstoneControl : INBTSerializable<CompoundTag?> {
abstract var redstoneSetting: RedstoneSetting abstract var redstoneSetting: RedstoneSetting
abstract var redstoneSignal: Int abstract var redstoneSignal: Int

View File

@ -7,24 +7,31 @@ import net.minecraft.world.MenuProvider
import net.minecraft.world.entity.player.Inventory import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.entity.player.Player import net.minecraft.world.entity.player.Player
import net.minecraft.world.inventory.AbstractContainerMenu import net.minecraft.world.inventory.AbstractContainerMenu
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.state.BlockState import net.minecraft.world.level.block.state.BlockState
import ru.dbotthepony.mc.otm.block.entity.IRedstoneControlled import ru.dbotthepony.mc.otm.block.entity.IRedstoneControlled
import ru.dbotthepony.mc.otm.block.entity.MatteryBlockEntity import ru.dbotthepony.mc.otm.block.entity.MatteryBlockEntity
import ru.dbotthepony.mc.otm.block.entity.SynchronizedRedstoneControl import ru.dbotthepony.mc.otm.block.entity.SynchronizedRedstoneControl
import ru.dbotthepony.mc.otm.core.nbt.set
import ru.dbotthepony.mc.otm.menu.decorative.HoloSignMenu import ru.dbotthepony.mc.otm.menu.decorative.HoloSignMenu
import ru.dbotthepony.mc.otm.once
import ru.dbotthepony.mc.otm.registry.MBlockEntities import ru.dbotthepony.mc.otm.registry.MBlockEntities
import ru.dbotthepony.mc.otm.registry.MBlocks import ru.dbotthepony.mc.otm.registry.MBlocks
class HoloSignBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBlockEntity(MBlockEntities.HOLO_SIGN, blockPos, blockState), MenuProvider, IRedstoneControlled { class HoloSignBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBlockEntity(MBlockEntities.HOLO_SIGN, blockPos, blockState), MenuProvider, IRedstoneControlled {
override val redstoneControl = SynchronizedRedstoneControl(synchronizer) { _, _ -> setChanged() } override val redstoneControl = SynchronizedRedstoneControl(synchronizer) { _, _ -> setChanged() }
var text by synchronizer.string("", name = "text", setter = { value, access, remote -> var signText by synchronizer.string("", name = "text", setter = { value, access, remote ->
setChanged() setChanged()
access.write(value) access.write(value)
}) })
var locked = false var isLocked = false
init {
savetables.string(::signText)
savetables.bool(::isLocked)
savetables.stateful(::redstoneControl)
}
override fun createMenu(p_39954_: Int, p_39955_: Inventory, p_39956_: Player): AbstractContainerMenu { override fun createMenu(p_39954_: Int, p_39955_: Inventory, p_39956_: Player): AbstractContainerMenu {
return HoloSignMenu(p_39954_, p_39955_, this) return HoloSignMenu(p_39954_, p_39955_, this)
@ -34,29 +41,24 @@ class HoloSignBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryB
return MBlocks.HOLO_SIGN.name return MBlocks.HOLO_SIGN.name
} }
override fun saveAdditional(nbt: CompoundTag) { override fun setLevel(level: Level) {
super.saveAdditional(nbt) super.setLevel(level)
nbt[TEXT_KEY] = text
nbt[REDSTONE_CONTROL_KEY] = redstoneControl.serializeNBT() level.once {
if (!isRemoved && this.level == level) {
redstoneControl.redstoneSignal = level.getBestNeighborSignal(blockPos)
}
}
} }
override fun load(nbt: CompoundTag) { override fun load(nbt: CompoundTag) {
super.load(nbt) super.load(nbt)
locked = nbt.getBoolean(LOCKED_KEY)
if (locked) { if (!isLocked)
text = nbt.getString(TEXT_KEY) signText = truncate(signText)
} else {
text = truncate(nbt.getString(TEXT_KEY))
}
redstoneControl.deserializeNBT(nbt[REDSTONE_CONTROL_KEY] as? CompoundTag)
} }
companion object { companion object {
const val TEXT_KEY = "SignText"
const val LOCKED_KEY = "Locked"
const val REDSTONE_CONTROL_KEY = "RedstoneControl"
const val DEFAULT_MAX_NEWLINES = 8 const val DEFAULT_MAX_NEWLINES = 8
const val DEFAULT_MAX_LINE_LENGTH = 15 const val DEFAULT_MAX_LINE_LENGTH = 15
private val NEWLINES = Regex("\r?\n") private val NEWLINES = Regex("\r?\n")

View File

@ -4,16 +4,12 @@ import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.client.renderer.MultiBufferSource import net.minecraft.client.renderer.MultiBufferSource
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer import net.minecraft.client.renderer.blockentity.BlockEntityRenderer
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider
import net.minecraft.core.Direction
import ru.dbotthepony.mc.otm.block.RotatableMatteryBlock
import ru.dbotthepony.mc.otm.block.entity.decorative.HoloSignBlockEntity import ru.dbotthepony.mc.otm.block.entity.decorative.HoloSignBlockEntity
import ru.dbotthepony.mc.otm.client.font import ru.dbotthepony.mc.otm.client.font
import ru.dbotthepony.mc.otm.client.render.DynamicBufferSource import ru.dbotthepony.mc.otm.client.render.DynamicBufferSource
import ru.dbotthepony.mc.otm.client.render.TextAlign import ru.dbotthepony.mc.otm.client.render.TextAlign
import ru.dbotthepony.mc.otm.client.render.drawAligned import ru.dbotthepony.mc.otm.client.render.drawAligned
import ru.dbotthepony.mc.otm.core.get
import ru.dbotthepony.mc.otm.core.math.RGBAColor import ru.dbotthepony.mc.otm.core.math.RGBAColor
import ru.dbotthepony.mc.otm.core.math.facingTwo
import ru.dbotthepony.mc.otm.core.math.rotateWithBlockFacing import ru.dbotthepony.mc.otm.core.math.rotateWithBlockFacing
import ru.dbotthepony.mc.otm.core.math.rotationThree import ru.dbotthepony.mc.otm.core.math.rotationThree
@ -36,7 +32,7 @@ class HoloSignRenderer(private val context: BlockEntityRendererProvider.Context)
val sorse = DynamicBufferSource.WORLD val sorse = DynamicBufferSource.WORLD
val lines = tile.text.split('\n') val lines = tile.signText.split('\n')
val totalHeight = lines.size * font.lineHeight + (lines.size - 1) * 2f val totalHeight = lines.size * font.lineHeight + (lines.size - 1) * 2f
var y = -totalHeight / 2f var y = -totalHeight / 2f

View File

@ -135,6 +135,18 @@ class Savetables : INBTSerializable<CompoundTag> {
.withDeserializer { it.asByte > 0 } .withDeserializer { it.asByte > 0 }
} }
fun string(prop: GetterSetter<String>, name: String): Stateless<String, StringTag> {
return Stateless(prop, name, StringTag::class.java)
.withSerializer { StringTag.valueOf(it) }
.withDeserializer { it.asString }
}
fun string(prop: KMutableProperty0<String>, name: String = prop.name): Stateless<String, StringTag> {
return Stateless(prop, name, StringTag::class.java)
.withSerializer { StringTag.valueOf(it) }
.withDeserializer { it.asString }
}
fun <E : Enum<E>> enum(prop: GetterSetter<E>, name: String, map: (String) -> E): Stateless<E, StringTag> { fun <E : Enum<E>> enum(prop: GetterSetter<E>, name: String, map: (String) -> E): Stateless<E, StringTag> {
return Stateless(prop, name, StringTag::class.java) return Stateless(prop, name, StringTag::class.java)
.withSerializer { StringTag.valueOf(it.name) } .withSerializer { StringTag.valueOf(it.name) }

View File

@ -25,8 +25,8 @@ class HoloSignMenu @JvmOverloads constructor(
locked.filter { it.isCreative } locked.filter { it.isCreative }
if (tile != null) { if (tile != null) {
text.withConsumer { if (tile.locked) tile.text = it else tile.text = HoloSignBlockEntity.truncate(it) }.withSupplier(tile::text) text.withConsumer { if (tile.isLocked) tile.signText = it else tile.signText = HoloSignBlockEntity.truncate(it) }.withSupplier(tile::signText)
locked.with(tile::locked) locked.with(tile::isLocked)
redstone.with(tile.redstoneControl::redstoneSetting) redstone.with(tile.redstoneControl::redstoneSetting)
} }
} }