forked from mirrors/cobblemon
Fixes, tmm multiplayer syncing and cleanup
This commit is contained in:
@@ -35,6 +35,7 @@ import com.cobblemon.mod.common.client.net.gui.PokedexUIPacketHandler
|
||||
import com.cobblemon.mod.common.client.net.gui.SetActiveTMPacketHandler
|
||||
import com.cobblemon.mod.common.client.net.gui.SetTMMachineContainerDataPacketHandler
|
||||
import com.cobblemon.mod.common.client.net.gui.SummaryUIPacketHandler
|
||||
import com.cobblemon.mod.common.client.net.gui.SynchronizeActiveTMHandler
|
||||
import com.cobblemon.mod.common.client.net.habitat.OpenHabitatBlockEditorHandler
|
||||
import com.cobblemon.mod.common.client.net.npc.CloseNPCEditorHandler
|
||||
import com.cobblemon.mod.common.client.net.npc.OpenNPCEditorHandler
|
||||
@@ -147,6 +148,7 @@ import com.cobblemon.mod.common.net.messages.client.ui.PokedexUIPacket
|
||||
import com.cobblemon.mod.common.net.messages.client.ui.SetActiveTMPacket
|
||||
import com.cobblemon.mod.common.net.messages.client.ui.SetTMMachineContainerDataPacket
|
||||
import com.cobblemon.mod.common.net.messages.client.ui.SummaryUIPacket
|
||||
import com.cobblemon.mod.common.net.messages.client.ui.SynchronizeActiveTMPacket
|
||||
import com.cobblemon.mod.common.net.messages.server.BattleChallengePacket
|
||||
import com.cobblemon.mod.common.net.messages.server.BattleChallengeResponsePacket
|
||||
import com.cobblemon.mod.common.net.messages.server.BenchMovePacket
|
||||
@@ -375,6 +377,7 @@ object CobblemonNetwork {
|
||||
list.add(PacketRegisterInfo(PlayerInteractOptionsPacket.ID, PlayerInteractOptionsPacket::decode, PlayerInteractOptionsHandler))
|
||||
list.add(PacketRegisterInfo(PokedexUIPacket.ID, PokedexUIPacket::decode, PokedexUIPacketHandler))
|
||||
list.add(PacketRegisterInfo(ExpGainedDataPacket.ID, ExpGainedDataPacket::decode, ExpGainedDataPacketHandler))
|
||||
list.add(PacketRegisterInfo(SynchronizeActiveTMPacket.ID, SynchronizeActiveTMPacket::decode, SynchronizeActiveTMHandler))
|
||||
|
||||
// Starter packets
|
||||
list.add(PacketRegisterInfo(OpenStarterUIPacket.ID, OpenStarterUIPacket::decode, StarterUIPacketHandler))
|
||||
|
||||
@@ -10,11 +10,13 @@ package com.cobblemon.mod.common.block.tmmachine
|
||||
|
||||
import com.cobblemon.mod.common.CobblemonItems
|
||||
import com.cobblemon.mod.common.CobblemonMenuType
|
||||
import com.cobblemon.mod.common.CobblemonNetwork.sendPacket
|
||||
import com.cobblemon.mod.common.api.moves.Moves
|
||||
import com.cobblemon.mod.common.api.tms.TechnicalMachine
|
||||
import com.cobblemon.mod.common.api.tms.TechnicalMachines
|
||||
import com.cobblemon.mod.common.block.entity.TMMachineBlockEntity
|
||||
import com.cobblemon.mod.common.util.itemRegistry
|
||||
import net.minecraft.server.level.ServerLevel
|
||||
import com.cobblemon.mod.common.net.messages.client.ui.SynchronizeActiveTMPacket
|
||||
import net.minecraft.server.level.ServerPlayer
|
||||
import net.minecraft.util.Mth
|
||||
import net.minecraft.world.Container
|
||||
import net.minecraft.world.SimpleContainer
|
||||
@@ -35,38 +37,20 @@ class TMMachineMenu(menuType: MenuType<*>, syncId: Int) : AbstractContainerMenu(
|
||||
var inventory: Container? = null
|
||||
var containerData: ContainerData? = null
|
||||
|
||||
var currentlySelectedTM: TechnicalMachine? = null
|
||||
private var lastSelectedTM: TechnicalMachine? = null
|
||||
|
||||
var onSelectedTMUpdate: (TechnicalMachine?) -> Unit = {}
|
||||
|
||||
constructor(syncId: Int, playerInventory: Inventory) : this(CobblemonMenuType.TM_MACHINE, syncId) {
|
||||
this.playerInventory = playerInventory
|
||||
this.inventory = SimpleContainer(5)
|
||||
this.containerData = SimpleContainerData(6)
|
||||
this.tmMachineEntity = (inventory as? TMMachineBlockEntity.TMMachineBlockInventory)?.blockEntity
|
||||
|
||||
this.addDataSlots(containerData!!)
|
||||
inventory?.startOpen(playerInventory.player)
|
||||
|
||||
val startX = 15
|
||||
val startY = 144
|
||||
val slotWidth = 18
|
||||
val slotHeight = 18
|
||||
|
||||
for (row in 0..2) {
|
||||
for (col in 0..8) {
|
||||
this.addSlot(Slot(playerInventory, 9 + (row * 9) + col, startX + (slotWidth * col), startY + (slotHeight * row)))
|
||||
}
|
||||
}
|
||||
for (col in 0..8) {
|
||||
this.addSlot(Slot(playerInventory, col, startX + (slotWidth * col), startY + 58))
|
||||
}
|
||||
|
||||
val container = inventory
|
||||
container?.let {
|
||||
this.addSlot(TMResultSlot(container, RESULT_SLOT, startX + 37, startY - 28)) // Output slot
|
||||
this.addSlot(BlankTMSlot(container, BLANK_TM_SLOT, startX + 90, startY - 28)) // Blank TM
|
||||
for (index in INGREDIENT_SLOTS) {
|
||||
this.addSlot(Slot(container, index, startX + 114 + (18 * (index - INGREDIENT_SLOTS.first)), startY - 28))
|
||||
}
|
||||
}
|
||||
this.addSlotListener(this)
|
||||
initiateSlots()
|
||||
}
|
||||
|
||||
constructor(syncId: Int, playerInventory: Inventory, inventory: Container, containerData: ContainerData) : this(CobblemonMenuType.TM_MACHINE, syncId) {
|
||||
@@ -78,6 +62,20 @@ class TMMachineMenu(menuType: MenuType<*>, syncId: Int) : AbstractContainerMenu(
|
||||
this.addDataSlots(containerData)
|
||||
inventory.startOpen(playerInventory.player)
|
||||
|
||||
initiateSlots()
|
||||
|
||||
tmMachineEntity?.let {
|
||||
val move = Moves.getByName(it.activeMove)
|
||||
move.let {
|
||||
currentlySelectedTM = TechnicalMachines.moveToTM[it]
|
||||
synchronizeSelectedTM()
|
||||
}
|
||||
}
|
||||
|
||||
this.addSlotListener(this)
|
||||
}
|
||||
|
||||
private fun initiateSlots() {
|
||||
val startX = 15
|
||||
val startY = 144
|
||||
val slotWidth = 18
|
||||
@@ -92,7 +90,7 @@ class TMMachineMenu(menuType: MenuType<*>, syncId: Int) : AbstractContainerMenu(
|
||||
this.addSlot(Slot(playerInventory, col, startX + (slotWidth * col), startY + 58))
|
||||
}
|
||||
|
||||
val container = inventory
|
||||
val container = inventory!!
|
||||
container.let {
|
||||
this.addSlot(TMResultSlot(container, RESULT_SLOT, startX + 37, startY - 28)) // Output slot
|
||||
this.addSlot(BlankTMSlot(container, BLANK_TM_SLOT, startX + 90, startY - 28)) // Blank TM
|
||||
@@ -100,7 +98,6 @@ class TMMachineMenu(menuType: MenuType<*>, syncId: Int) : AbstractContainerMenu(
|
||||
this.addSlot(Slot(container, index, startX + 114 + (18 * (index - INGREDIENT_SLOTS.first)), startY - 28))
|
||||
}
|
||||
}
|
||||
this.addSlotListener(this)
|
||||
}
|
||||
|
||||
override fun quickMoveStack(player: Player, index: Int): ItemStack {
|
||||
@@ -207,4 +204,25 @@ class TMMachineMenu(menuType: MenuType<*>, syncId: Int) : AbstractContainerMenu(
|
||||
override fun dataChanged(containerMenu: AbstractContainerMenu, dataSlotIndex: Int, value: Int) {
|
||||
broadcastChanges()
|
||||
}
|
||||
|
||||
fun synchronizeSelectedTM() {
|
||||
val serverPlayer = playerInventory?.player as? ServerPlayer ?: return
|
||||
serverPlayer.sendPacket(SynchronizeActiveTMPacket(currentlySelectedTM))
|
||||
lastSelectedTM = currentlySelectedTM
|
||||
}
|
||||
|
||||
override fun sendAllDataToRemote() {
|
||||
super.sendAllDataToRemote()
|
||||
synchronizeSelectedTM()
|
||||
}
|
||||
|
||||
override fun broadcastChanges() {
|
||||
super.broadcastChanges()
|
||||
if (lastSelectedTM != currentlySelectedTM) synchronizeSelectedTM()
|
||||
}
|
||||
|
||||
fun clientSetSelectedTM(tm: TechnicalMachine?) {
|
||||
currentlySelectedTM = tm
|
||||
onSelectedTMUpdate(tm)
|
||||
}
|
||||
}
|
||||
|
||||
+50
-71
@@ -120,7 +120,7 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
fun getLogoResource(langCode: String?): ResourceLocation =
|
||||
logoResourceMap.entries.find { it.value.contains(langCode?.substringBefore("_")) }?.key ?: logo
|
||||
}
|
||||
var selectedTM: TechnicalMachine? = null
|
||||
|
||||
var heldStackMove: MoveTemplate? = null
|
||||
var tmList: SettableObservable<MutableList<TechnicalMachine>> = SettableObservable(mutableListOf())
|
||||
var output: ItemStack? = null
|
||||
@@ -159,6 +159,8 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
var oldIngredientItems: Array<Item> = arrayOf(Items.AIR, Items.AIR, Items.AIR)
|
||||
var oldResultTmSlotItem: Item = Items.AIR
|
||||
|
||||
var moveInfoTM: TechnicalMachine? = null
|
||||
|
||||
override fun init() {
|
||||
imageWidth = WIDTH
|
||||
imageHeight = HEIGHT
|
||||
@@ -171,6 +173,8 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
logoPosY = topPos + 1
|
||||
scanPosY = topPos + 1
|
||||
|
||||
menu.onSelectedTMUpdate = { updateSelectedTM(it) }
|
||||
|
||||
partySlotList.clear()
|
||||
CobblemonClient.storage.party.forEachIndexed { index, pokemon ->
|
||||
partySlotList.add(TMPartySlotWidget(
|
||||
@@ -226,7 +230,14 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
pX = leftPos + 4,
|
||||
pY = topPos + 29,
|
||||
tmList,
|
||||
setTM = { tm, clicked -> setSelectedTM(tm, clicked) }
|
||||
setTM = { tm, clicked ->
|
||||
if (clicked) {
|
||||
if (tm != menu.currentlySelectedTM) SetActiveTMPacket(tm).sendToServer()
|
||||
setScreenFromMode(TM_BURN_MODE)
|
||||
} else {
|
||||
updateMoveInformation(tm)
|
||||
}
|
||||
}
|
||||
)
|
||||
if (!children().contains(movesScrollingList)) addRenderableWidget(movesScrollingList)
|
||||
|
||||
@@ -252,24 +263,21 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
selectedMoveButton = MoveSlotButton(
|
||||
leftPos + 6,
|
||||
topPos + 6,
|
||||
move = selectedTM?.moveName,
|
||||
pp = selectedTM?.moveName?.pp ?: 0,
|
||||
move = menu.currentlySelectedTM?.moveName,
|
||||
pp = menu.currentlySelectedTM?.moveName?.pp ?: 0,
|
||||
ppAsFraction = false
|
||||
) {
|
||||
if ((it as MoveSlotButton).enabled) {
|
||||
if (::movesScrollingList.isInitialized && movesScrollingList.children().isEmpty()) setMoveList()
|
||||
playSound(CobblemonSounds.GUI_CLICK)
|
||||
setScreenFromMode(MOVE_SELECT_MODE)
|
||||
|
||||
// Clear active TM
|
||||
CobblemonNetwork.sendToServer(SetActiveTMPacket(null))
|
||||
}
|
||||
}
|
||||
if (!children().contains(selectedMoveButton)) addRenderableWidget(selectedMoveButton)
|
||||
|
||||
startButton = StartButton(leftPos + 5F, topPos + 35F) {
|
||||
if (!(it as StartButton).disabled) {
|
||||
selectedTM?.let {
|
||||
menu.currentlySelectedTM?.let {
|
||||
val burnActive = menu.containerData?.get(TMMachineBlockEntity.BURN_ACTIVE_INDEX) ?: 0
|
||||
CobblemonNetwork.sendToServer(SetTMMachineContainerDataPacket(
|
||||
TMMachineBlockEntity.BURN_ACTIVE_INDEX,
|
||||
@@ -284,17 +292,14 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
))
|
||||
}*/
|
||||
|
||||
CobblemonNetwork.sendToServer(SetActiveTMPacket(it))
|
||||
//CobblemonNetwork.sendToServer(SetActiveTMPacket(it))
|
||||
if (burnActive != 1) playSound(CobblemonSounds.TM_MACHINE_START)
|
||||
inventory.setChanged()
|
||||
menu.broadcastChanges()
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!children().contains(startButton)) addRenderableWidget(startButton)
|
||||
|
||||
setScreenFromMode(mode)
|
||||
initScreen = false
|
||||
|
||||
updateSlotItemMemory()
|
||||
}
|
||||
@@ -306,7 +311,7 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
if (scanPosY + 1 >= (topPos + SCREEN_SAVER_HEIGHT) * 2) scanPosY = 0
|
||||
|
||||
if (mode == TM_BURN_MODE) {
|
||||
selectedTM?.let { tm ->
|
||||
menu.currentlySelectedTM?.let { tm ->
|
||||
val resultStack = menu.inventory!!.getItem(TMMachineMenu.RESULT_SLOT)
|
||||
val craftedTmStack = ItemStack(CobblemonItems.TECHNICAL_MACHINE)
|
||||
.also { TMMoveComponent.setTMMove(it, tm.moveName) }
|
||||
@@ -339,7 +344,7 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
}
|
||||
|
||||
if (::selectedMoveButton.isInitialized) {
|
||||
selectedMoveButton.enabled = !isBurnActive() && (heldStackMove == null || heldStackMove == selectedTM?.moveName)
|
||||
selectedMoveButton.enabled = (heldStackMove == null || heldStackMove == menu.currentlySelectedTM?.moveName)
|
||||
}
|
||||
|
||||
if (isBurnActive()) {
|
||||
@@ -350,9 +355,6 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Check if screen should be switched upon opening UI
|
||||
if (!initScreen) loadBurnScreenData()
|
||||
}
|
||||
|
||||
if (heldMoveItemChanged) {
|
||||
@@ -400,32 +402,6 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
return wasScreenSaverActive
|
||||
}
|
||||
|
||||
private fun loadBurnScreenData() {
|
||||
var tm: TechnicalMachine? = null
|
||||
|
||||
val blockPos = menu.containerData?.let {
|
||||
val x = it.get(TMMachineBlockEntity.POSITION_X_INDEX)
|
||||
val y = it.get(TMMachineBlockEntity.POSITION_Y_INDEX)
|
||||
val z = it.get(TMMachineBlockEntity.POSITION_Z_INDEX)
|
||||
BlockPos(x, y, z)
|
||||
}
|
||||
|
||||
inventory.player.level().getBlockEntity(blockPos)?.let { blockEntity ->
|
||||
if (blockEntity is TMMachineBlockEntity) {
|
||||
Moves.getByName(blockEntity.activeMove)?.let {
|
||||
tm = TechnicalMachines.moveToTM[it]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If burn currently in progress or if active TM set, set to burn screen
|
||||
if (isBurnActive() || tm != null) {
|
||||
setSelectedTM(tm, true)
|
||||
}
|
||||
|
||||
initScreen = true
|
||||
}
|
||||
|
||||
private fun isBurnActive(): Boolean = (menu.containerData?.get(TMMachineBlockEntity.BURN_ACTIVE_INDEX) ?: 0) == 1
|
||||
|
||||
private fun setScreenFromMode(screen: Int) {
|
||||
@@ -467,7 +443,6 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
|
||||
private fun toggleTypeSelect(isVisible: Boolean) {
|
||||
if (isVisible) {
|
||||
setSelectedTM(null, false)
|
||||
if (::moveSearchWidget.isInitialized) moveSearchWidget.value = ""
|
||||
if (selectedPokemon != null) selectedPokemon = null
|
||||
sortType = null
|
||||
@@ -505,7 +480,6 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
}
|
||||
|
||||
private fun toggleTMBurn(isVisible: Boolean) {
|
||||
if (!isVisible) setSelectedTM(null, false)
|
||||
if (::selectedMoveButton.isInitialized) selectedMoveButton.visible = isVisible
|
||||
if (::startButton.isInitialized) startButton.visible = isVisible
|
||||
}
|
||||
@@ -516,35 +490,40 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
return if (learnableMoves.contains(move)) TMPartySlotWidget.CAN_LEARN else TMPartySlotWidget.CANNOT_LEARN
|
||||
}
|
||||
|
||||
fun setSelectedTM(tm: TechnicalMachine?, clicked: Boolean) {
|
||||
if (selectedTM != tm) {
|
||||
selectedTM = tm
|
||||
updatePartySlotStatus(tm?.moveName)
|
||||
fun updateSelectedTM(tm: TechnicalMachine?) {
|
||||
if (!initScreen) {
|
||||
initScreen = true
|
||||
setScreenFromMode(TM_BURN_MODE)
|
||||
tm?.let {
|
||||
sortType = ElementalTypes.get(tm.type)
|
||||
setMoveList()
|
||||
}
|
||||
}
|
||||
|
||||
if (::movesScrollingList.isInitialized) {
|
||||
movesScrollingList.setSlotHighlighted(if (clicked) null else selectedTM?.id)
|
||||
if (mode == TM_BURN_MODE) updateMoveInformation(tm)
|
||||
|
||||
tm?.let {
|
||||
if (::selectedMoveButton.isInitialized) {
|
||||
selectedMoveButton.apply {
|
||||
move = it.moveName
|
||||
pp = it.moveName.pp
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateMoveInformation(tm: TechnicalMachine?) {
|
||||
moveInfoTM = tm
|
||||
|
||||
if (::moveDescriptionWidget.isInitialized) {
|
||||
val selectedDescription = selectedTM?.moveName?.description?.string
|
||||
val selectedDescription = tm?.moveName?.description?.string
|
||||
moveDescriptionWidget.setText(
|
||||
if (selectedDescription != null) listOf(selectedDescription) else emptyList()
|
||||
)
|
||||
}
|
||||
|
||||
if (clicked) {
|
||||
CobblemonNetwork.sendToServer(SetActiveTMPacket(tm))
|
||||
|
||||
selectedTM?.let {
|
||||
if (::selectedMoveButton.isInitialized) {
|
||||
selectedMoveButton.apply {
|
||||
move = it.moveName
|
||||
pp = it.moveName.pp
|
||||
}
|
||||
setScreenFromMode(TM_BURN_MODE)
|
||||
}
|
||||
}
|
||||
if (::movesScrollingList.isInitialized) {
|
||||
movesScrollingList.setSlotHighlighted(tm?.id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -557,7 +536,7 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
}
|
||||
|
||||
fun renderDiscTypeOverlay(poseStack: PoseStack, isDiscRotating: Boolean, alpha: Float = 1F) {
|
||||
selectedTM?.let {
|
||||
menu.currentlySelectedTM?.let {
|
||||
val primaryRgb = it.moveName.elementalType.primaryColor.toRGB()
|
||||
val secondaryRgb = it.moveName.elementalType.secondaryColor.toRGB()
|
||||
blitk(
|
||||
@@ -706,7 +685,7 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
}
|
||||
|
||||
fun renderMoveInfo(context: GuiGraphics, mouseX: Int, mouseY: Int) {
|
||||
val moveTemplate = selectedTM?.moveName
|
||||
val moveTemplate = moveInfoTM?.moveName
|
||||
|
||||
blitk(
|
||||
matrixStack = context.pose(),
|
||||
@@ -1057,7 +1036,7 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
movesScrollingList.setDisabled(true, TechnicalMachines.moveToTM[heldStackMove])
|
||||
if (mode == MOVE_SELECT_MODE) {
|
||||
val heldStackTm = TechnicalMachines.moveToTM[heldStackMove]
|
||||
setSelectedTM(if (tmList.get().contains(heldStackTm)) heldStackTm else null, false)
|
||||
updateMoveInformation(if (tmList.get().contains(heldStackTm)) heldStackTm else null)
|
||||
|
||||
// Scroll to move if moves list contains held TM move
|
||||
if (tmList.get().contains(heldStackTm)) {
|
||||
@@ -1077,8 +1056,8 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
heldStackMove = null
|
||||
|
||||
// Reset selected move on move select screen
|
||||
if (mode == MOVE_SELECT_MODE) setSelectedTM(null, false)
|
||||
updatePartySlotStatus(selectedTM?.moveName)
|
||||
if (mode == MOVE_SELECT_MODE) updateMoveInformation(null)
|
||||
updatePartySlotStatus(menu.currentlySelectedTM?.moveName)
|
||||
|
||||
if (::movesScrollingList.isInitialized) {
|
||||
movesScrollingList.setDisabled(false)
|
||||
@@ -1155,7 +1134,7 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
if (isInventoryKeyPressed(minecraft, keyCode, scanCode)) {
|
||||
if (searchFocused) { return true }
|
||||
else {
|
||||
Minecraft.getInstance().setScreen(null)
|
||||
onClose()
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -1166,7 +1145,7 @@ class TMMachineScreen(containerMenu: TMMachineMenu, val inventory: Inventory, ti
|
||||
this.focused = null
|
||||
return true
|
||||
} else {
|
||||
Minecraft.getInstance().setScreen(null)
|
||||
onClose()
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
+11
-4
@@ -19,7 +19,6 @@ import net.minecraft.world.level.block.Block
|
||||
object SetActiveTMPacketHandler : ServerNetworkPacketHandler<SetActiveTMPacket> {
|
||||
override fun handle(packet: SetActiveTMPacket, server: MinecraftServer, player: ServerPlayer) {
|
||||
val menu = player.containerMenu as? TMMachineMenu ?: return
|
||||
val inventory = menu.inventory ?: return
|
||||
val tmData = Cobblemon.playerDataManager.getTMData(player)
|
||||
|
||||
if (packet.tm != null && (!packet.tm.isPassivelyObtained() && packet.tm.id !in tmData.learnedTMs)) {
|
||||
@@ -30,9 +29,17 @@ object SetActiveTMPacketHandler : ServerNetworkPacketHandler<SetActiveTMPacket>
|
||||
blockEntity.activeMove = packet.tm?.moveName?.name ?: ""
|
||||
blockEntity.setChanged()
|
||||
player.level().sendBlockUpdated(blockEntity.blockPos, blockEntity.blockState, blockEntity.blockState, Block.UPDATE_CLIENTS)
|
||||
}
|
||||
|
||||
inventory.setChanged()
|
||||
menu.broadcastChanges()
|
||||
blockEntity.burnActive = false
|
||||
blockEntity.burnProgress = 0
|
||||
|
||||
player.level().players().forEach { thePlayer -> //Update all players with matching menu/blockentity
|
||||
val tmmMenu = thePlayer.containerMenu as? TMMachineMenu ?: return@forEach
|
||||
if (tmmMenu.tmMachineEntity == blockEntity) {
|
||||
tmmMenu.currentlySelectedTM = packet.tm
|
||||
tmmMenu.broadcastChanges()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.cobblemon.mod.common.client.net.gui
|
||||
|
||||
import com.cobblemon.mod.common.api.net.ClientNetworkPacketHandler
|
||||
import com.cobblemon.mod.common.block.tmmachine.TMMachineMenu
|
||||
import com.cobblemon.mod.common.net.messages.client.ui.SynchronizeActiveTMPacket
|
||||
import net.minecraft.client.Minecraft
|
||||
|
||||
object SynchronizeActiveTMHandler: ClientNetworkPacketHandler<SynchronizeActiveTMPacket> {
|
||||
override fun handle(packet: SynchronizeActiveTMPacket, client: Minecraft) {
|
||||
val tmmMenu = client.player?.containerMenu as? TMMachineMenu ?: return
|
||||
tmmMenu.clientSetSelectedTM(packet.tm)
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.cobblemon.mod.common.net.messages.client.ui
|
||||
|
||||
import com.cobblemon.mod.common.api.net.NetworkPacket
|
||||
import com.cobblemon.mod.common.api.tms.TechnicalMachine
|
||||
import com.cobblemon.mod.common.api.tms.TechnicalMachines
|
||||
import com.cobblemon.mod.common.util.cobblemonResource
|
||||
import net.minecraft.network.RegistryFriendlyByteBuf
|
||||
|
||||
class SynchronizeActiveTMPacket(val tm: TechnicalMachine?): NetworkPacket<SynchronizeActiveTMPacket> {
|
||||
override val id = ID
|
||||
|
||||
override fun encode(buffer: RegistryFriendlyByteBuf) {
|
||||
buffer.writeNullable(tm?.id) { pb, value -> pb.writeResourceLocation(value) }
|
||||
}
|
||||
|
||||
companion object {
|
||||
val ID = cobblemonResource("synchronize_active_tm")
|
||||
fun decode(buffer: RegistryFriendlyByteBuf): SynchronizeActiveTMPacket {
|
||||
val resourceLocation = buffer.readNullable { it.readResourceLocation() }
|
||||
if (resourceLocation == null) return SynchronizeActiveTMPacket(null)
|
||||
val tm = TechnicalMachines.tmMap[resourceLocation]
|
||||
return SynchronizeActiveTMPacket(tm)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user