|
1 | 1 | package com.teammoeg.frostedheart.content.town.resident; |
2 | 2 |
|
| 3 | +import com.teammoeg.chorda.client.cui.base.MouseButton; |
| 4 | +import com.teammoeg.chorda.client.cui.base.TooltipBuilder; |
3 | 5 | import com.teammoeg.chorda.client.cui.widgets.TextButton; |
4 | 6 | import com.teammoeg.chorda.client.icon.FlatIcon; |
| 7 | +import com.teammoeg.chorda.dataholders.team.CClientTeamDataManager; |
5 | 8 | import com.teammoeg.frostedheart.FHNetwork; |
| 9 | +import com.teammoeg.frostedheart.bootstrap.common.FHSpecialDataTypes; |
| 10 | +import com.teammoeg.frostedheart.content.town.TeamTownData; |
| 11 | +import com.teammoeg.frostedheart.content.town.event.ITownDataUpdateListener; |
6 | 12 | import com.teammoeg.frostedheart.content.town.network.WanderingRefugeeOpenTradeGUIMessage; |
7 | 13 | import com.teammoeg.frostedheart.content.town.network.WanderingRefugeeRecruitMessage; |
8 | 14 | import com.teammoeg.frostedheart.content.ui.dialogue.DialogueOverlay; |
|
13 | 19 |
|
14 | 20 | @OnlyIn(Dist.CLIENT) |
15 | 21 | public class WanderingRefugeeClientHelper { |
| 22 | + /** 当前对话注册的城镇数据监听器,对话关闭时自我移除,避免跨对话累积。 */ |
| 23 | + private static ITownDataUpdateListener refreshListener = null; |
| 24 | + |
16 | 25 | public static void openScreen(WanderingRefugee entity) { |
17 | 26 | var trade = TextButton.create(DialogueOverlay.INSTANCE, Component.translatable("gui.frostedheart.wandering_refugee.trade_button"), FlatIcon.TRADE.toCIcon(), mb -> |
18 | 27 | FHNetwork.INSTANCE.sendToServer(new WanderingRefugeeOpenTradeGUIMessage(entity.getId())) |
19 | 28 | ); |
20 | | - var recruit = TextButton.create(DialogueOverlay.INSTANCE, Component.translatable("gui.frostedheart.wandering_refugee.recruit_button"), FlatIcon.GAIN.toCIcon(), mb -> |
21 | | - FHNetwork.INSTANCE.sendToServer(new WanderingRefugeeRecruitMessage(entity.getId())) |
22 | | - ); |
23 | | -// recruit.setEnabled(false); TODO 无法容纳更多居民时禁用按钮 |
| 29 | + // 招募按钮:禁用时显示"无空余房屋"提示,状态翻转时提示自动跟随 |
| 30 | + var recruit = new TextButton(DialogueOverlay.INSTANCE, Component.translatable("gui.frostedheart.wandering_refugee.recruit_button"), FlatIcon.GAIN.toCIcon()) { |
| 31 | + @Override |
| 32 | + public void onClicked(MouseButton button) { |
| 33 | + FHNetwork.INSTANCE.sendToServer(new WanderingRefugeeRecruitMessage(entity.getId())); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void getTooltip(TooltipBuilder list) { |
| 38 | + if (!isEnabled()) { |
| 39 | + list.accept(Component.translatable("gui.frostedheart.wandering_refugee.recruit_disabled_no_housing")); |
| 40 | + } |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + boolean canRecruit = canAddResident(); |
| 45 | + recruit.setEnabled(canRecruit); |
| 46 | + |
| 47 | + // 注册城镇数据增量监听:建筑/居民变化(新建房屋、居民死亡释放槽位等)时刷新招募可用状态。 |
| 48 | + // 复用与城镇 GUI 相同的 addClientListener/removeClientListener 机制;监听在对话关闭 |
| 49 | + // (DialogueOverlay 被禁用)时自我移除。 |
| 50 | + if (refreshListener != null) { |
| 51 | + TeamTownData.removeClientListener(refreshListener); |
| 52 | + } |
| 53 | + final TextButton recruitButton = recruit; |
| 54 | + refreshListener = new ITownDataUpdateListener() { |
| 55 | + @Override |
| 56 | + public void onBuildingsChanged() { |
| 57 | + refreshRecruit(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void onResidentsChanged() { |
| 62 | + refreshRecruit(); |
| 63 | + } |
| 64 | + |
| 65 | + private void refreshRecruit() { |
| 66 | + if (!DialogueOverlay.INSTANCE.isEnabled()) { |
| 67 | + TeamTownData.removeClientListener(this); |
| 68 | + refreshListener = null; |
| 69 | + return; |
| 70 | + } |
| 71 | + recruitButton.setEnabled(canAddResident()); |
| 72 | + } |
| 73 | + }; |
| 74 | + TeamTownData.addClientListener(refreshListener); |
| 75 | + |
24 | 76 | DialogueScreen.open(true, trade, recruit); |
25 | 77 | // Minecraft.getInstance().setScreen(new WanderingRefugeeScreen(entity)); |
26 | 78 | } |
| 79 | + |
| 80 | + /** |
| 81 | + * 从客户端城镇数据快照判断能否再招募一名居民。 |
| 82 | + * <p> |
| 83 | + * Whether another resident can be recruited, based on the client-side town data snapshot. |
| 84 | + * |
| 85 | + * @return 可招募则返回 true / true if another resident can be recruited |
| 86 | + */ |
| 87 | + private static boolean canAddResident() { |
| 88 | + TeamTownData townData = CClientTeamDataManager.INSTANCE.getInstance().getData(FHSpecialDataTypes.TOWN_DATA); |
| 89 | + return townData != null && townData.createTeamTown().canAddResident(); |
| 90 | + } |
27 | 91 | } |
0 commit comments