Use menu inputs for inventory/exopack slot filter changes

This commit is contained in:
DBotThePony 2023-07-29 17:02:30 +07:00
parent 092daf80d8
commit 651febed96
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 7 additions and 52 deletions

View File

@ -50,10 +50,8 @@ import ru.dbotthepony.mc.otm.core.util.NullValueCodec
import ru.dbotthepony.mc.otm.core.util.VarIntValueCodec
import ru.dbotthepony.mc.otm.menu.input.InstantBooleanInput
import ru.dbotthepony.mc.otm.network.MatteryPacket
import ru.dbotthepony.mc.otm.network.MatteryPlayerNetworkChannel
import ru.dbotthepony.mc.otm.network.MenuFieldPacket
import ru.dbotthepony.mc.otm.network.MenuNetworkChannel
import ru.dbotthepony.mc.otm.network.SetInventoryFilterPacket
import ru.dbotthepony.mc.otm.network.enqueueWork
import ru.dbotthepony.mc.otm.network.packetHandled
import ru.dbotthepony.mc.otm.network.sender
@ -179,6 +177,8 @@ abstract class MatteryMenu @JvmOverloads protected constructor(
fun bigDecimalInput(allowSpectators: Boolean = false, handler: (BigDecimal) -> Unit) = PlayerInput(BigDecimalValueCodec, allowSpectators, handler)
fun booleanInput(allowSpectators: Boolean = false, handler: (Boolean) -> Unit) = PlayerInput(BooleanValueCodec, allowSpectators, handler)
fun itemInput(allowSpectators: Boolean = false, handler: (Item) -> Unit) = PlayerInput(ItemValueCodec, allowSpectators, handler)
fun nullableItemInput(allowSpectators: Boolean = false, handler: (Item?) -> Unit) = PlayerInput(ItemValueCodec.nullable, allowSpectators, handler)
fun stringInput(allowSpectators: Boolean = false, handler: (String) -> Unit) = PlayerInput(BinaryStringCodec, allowSpectators, handler)
fun intInput(allowSpectators: Boolean = false, handler: (Int) -> Unit) = PlayerInput(VarIntValueCodec, allowSpectators, handler)
@ -270,11 +270,12 @@ abstract class MatteryMenu @JvmOverloads protected constructor(
if (mattery != null) {
if (container === inventory) {
if (slotIndex in mattery.regularSlotFilters.indices)
if (slotIndex in mattery.regularSlotFilters.indices) {
filter = GetterSetter.of(
getter = { mattery.regularSlotFilters[slotIndex].value },
setter = { MatteryPlayerNetworkChannel.sendToServer(SetInventoryFilterPacket(SetInventoryFilterPacket.Type.INVENTORY, slotIndex, it)) }
setter = nullableItemInput(true) { mattery.regularSlotFilters[slotIndex].value = it }::input
)
}
if (slotIndex in mattery.regularSlotChargeFlag.indices) {
val input = booleanInput(true) { mattery.regularSlotChargeFlag[slotIndex].boolean = it }
@ -287,7 +288,7 @@ abstract class MatteryMenu @JvmOverloads protected constructor(
} else if (container === mattery.exoPackContainer) {
filter = GetterSetter.of(
getter = { mattery.exoPackContainer.getSlotFilter(slotIndex) },
setter = { MatteryPlayerNetworkChannel.sendToServer(SetInventoryFilterPacket(SetInventoryFilterPacket.Type.EXOPACK, slotIndex, it)) }
setter = nullableItemInput(true) { mattery.exoPackContainer.setSlotFilter(slotIndex, it) }::input
)
val input = booleanInput(true) { if (it) mattery.exoPackSlotsChargeFlag.add(slotIndex) else mattery.exoPackSlotsChargeFlag.remove(slotIndex) }

View File

@ -561,52 +561,8 @@ data class SetExopackColorPacket(val color: RGBAColor) : MatteryPacket {
}
}
class SetInventoryFilterPacket(val type: Type, val slot: Int, val item: Item?) : MatteryPacket {
enum class Type {
INVENTORY,
EXOPACK
}
override fun write(buff: FriendlyByteBuf) {
buff.writeEnum(type)
buff.writeVarInt(slot)
if (item == null) {
buff.writeByte(0)
} else {
buff.writeByte(1)
buff.writeItemType(item)
}
}
override fun play(context: Supplier<NetworkEvent.Context>) {
context.packetHandled = true
val player = context.sender?.matteryPlayer ?: return
when (type) {
Type.INVENTORY -> {
if (slot in 0 until player.regularSlotFilters.size) {
player.regularSlotFilters[slot].value = item
}
}
Type.EXOPACK -> {
if (slot in 0 until player.exoPackContainer.containerSize) {
player.exoPackContainer.setSlotFilter(slot, item)
}
}
}
}
companion object {
fun read(buff: FriendlyByteBuf): SetInventoryFilterPacket {
return SetInventoryFilterPacket(buff.readEnum(Type::class.java), buff.readVarInt(), if (buff.readByte() > 0) buff.readItemType() else null)
}
}
}
object MatteryPlayerNetworkChannel : MatteryNetworkChannel(
version = "4",
version = "5",
name = "player"
) {
fun register() {
@ -643,7 +599,5 @@ object MatteryPlayerNetworkChannel : MatteryNetworkChannel(
add(DisableExopackGlowPacket::class, { DisableExopackGlowPacket }, PLAY_TO_SERVER)
add(ResetExopackColorPacket::class, { ResetExopackColorPacket }, PLAY_TO_SERVER)
add(SetExopackColorPacket::class, SetExopackColorPacket.Companion::read, PLAY_TO_SERVER)
add(SetInventoryFilterPacket::class, SetInventoryFilterPacket.Companion::read, PLAY_TO_SERVER)
}
}