|
| 1 | +package com.github.gtexpert.gtbm.integration.forestry.util; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.function.IntSupplier; |
| 6 | + |
| 7 | +import net.bdew.gendustry.api.ApiaryModifiers; |
| 8 | +import net.minecraft.client.Minecraft; |
| 9 | +import net.minecraft.client.renderer.BufferBuilder; |
| 10 | +import net.minecraft.client.renderer.GlStateManager; |
| 11 | +import net.minecraft.client.renderer.Tessellator; |
| 12 | +import net.minecraft.client.renderer.texture.TextureAtlasSprite; |
| 13 | +import net.minecraft.client.renderer.vertex.DefaultVertexFormats; |
| 14 | +import net.minecraft.client.resources.I18n; |
| 15 | +import net.minecraft.item.ItemStack; |
| 16 | +import net.minecraft.network.PacketBuffer; |
| 17 | +import net.minecraft.util.text.TextFormatting; |
| 18 | +import net.minecraftforge.fml.relauncher.Side; |
| 19 | +import net.minecraftforge.fml.relauncher.SideOnly; |
| 20 | + |
| 21 | +import gregtech.api.gui.IRenderContext; |
| 22 | +import gregtech.api.gui.Widget; |
| 23 | +import gregtech.api.util.Position; |
| 24 | +import gregtech.api.util.Size; |
| 25 | + |
| 26 | +import forestry.api.apiculture.*; |
| 27 | +import forestry.api.core.*; |
| 28 | + |
| 29 | +/** |
| 30 | + * Gendustry-style bee status widget. |
| 31 | + * Displays Forestry error icons (cycled) and shows climate/bee stats on hover. |
| 32 | + */ |
| 33 | +public class WidgetBeeStatus extends Widget { |
| 34 | + |
| 35 | + private static final int SYNC_TOOLTIP = 0; |
| 36 | + private static final int SYNC_ERRORS = 1; |
| 37 | + |
| 38 | + private final IBeeHousing housing; |
| 39 | + private final IBeeRoot beeRoot; |
| 40 | + private final ApiaryModifiers modifiers; |
| 41 | + private final IntSupplier euPerTickSupplier; |
| 42 | + |
| 43 | + // Client-side synced data |
| 44 | + private List<String> clientTooltip = new ArrayList<>(); |
| 45 | + private List<Short> clientErrorIds = new ArrayList<>(); |
| 46 | + |
| 47 | + // Server-side change tracking |
| 48 | + private int lastTooltipHash = Integer.MIN_VALUE; |
| 49 | + private int lastErrorHash = Integer.MIN_VALUE; |
| 50 | + |
| 51 | + public WidgetBeeStatus(int x, int y, IBeeHousing housing, IBeeRoot beeRoot, ApiaryModifiers modifiers, |
| 52 | + IntSupplier euPerTickSupplier) { |
| 53 | + super(new Position(x, y), new Size(16, 16)); |
| 54 | + this.housing = housing; |
| 55 | + this.beeRoot = beeRoot; |
| 56 | + this.modifiers = modifiers; |
| 57 | + this.euPerTickSupplier = euPerTickSupplier; |
| 58 | + } |
| 59 | + |
| 60 | + // ---- Server-side: sync ---- |
| 61 | + |
| 62 | + @Override |
| 63 | + public void detectAndSendChanges() { |
| 64 | + if (housing.getWorldObj() == null) return; |
| 65 | + |
| 66 | + // Sync error IDs (for icon display) |
| 67 | + IErrorLogic errorLogic = housing.getErrorLogic(); |
| 68 | + List<Short> errorIds = new ArrayList<>(); |
| 69 | + for (IErrorState error : errorLogic.getErrorStates()) { |
| 70 | + errorIds.add(error.getID()); |
| 71 | + } |
| 72 | + int errorHash = errorIds.hashCode(); |
| 73 | + if (errorHash != lastErrorHash) { |
| 74 | + lastErrorHash = errorHash; |
| 75 | + writeUpdateInfo(SYNC_ERRORS, buf -> { |
| 76 | + buf.writeVarInt(errorIds.size()); |
| 77 | + for (short id : errorIds) { |
| 78 | + buf.writeShort(id); |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + // Sync tooltip lines |
| 84 | + List<String> tooltip = buildTooltipLines(); |
| 85 | + int tooltipHash = tooltip.hashCode(); |
| 86 | + if (tooltipHash != lastTooltipHash) { |
| 87 | + lastTooltipHash = tooltipHash; |
| 88 | + writeUpdateInfo(SYNC_TOOLTIP, buf -> { |
| 89 | + buf.writeVarInt(tooltip.size()); |
| 90 | + for (String line : tooltip) { |
| 91 | + buf.writeString(line); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private List<String> buildTooltipLines() { |
| 98 | + List<String> lines = new ArrayList<>(); |
| 99 | + |
| 100 | + // Error descriptions (red) |
| 101 | + IErrorLogic errorLogic = housing.getErrorLogic(); |
| 102 | + for (IErrorState error : errorLogic.getErrorStates()) { |
| 103 | + lines.add(TextFormatting.RED + error.getUnlocalizedDescription()); |
| 104 | + } |
| 105 | + |
| 106 | + // Energy |
| 107 | + lines.add("gtbm.bee.label.energy\t" + euPerTickSupplier.getAsInt()); |
| 108 | + |
| 109 | + // Temperature & Humidity (send enum name for client-side localization) |
| 110 | + EnumTemperature temp = housing.getTemperature(); |
| 111 | + EnumHumidity hum = housing.getHumidity(); |
| 112 | + lines.add("gtbm.bee.label.temperature\t" + temp.getName().toLowerCase()); |
| 113 | + lines.add("gtbm.bee.label.humidity\t" + hum.getName().toLowerCase()); |
| 114 | + |
| 115 | + // Bee stats |
| 116 | + IBeeHousingInventory inv = housing.getBeeInventory(); |
| 117 | + if (beeRoot != null && !inv.getQueen().isEmpty()) { |
| 118 | + IBee bee = beeRoot.getMember(inv.getQueen()); |
| 119 | + if (bee != null && bee.isAnalyzed()) { |
| 120 | + IBeeGenome genome = bee.getGenome(); |
| 121 | + lines.add("gtbm.bee.label.production\t" + |
| 122 | + String.format("%.0f%%", 100F * modifiers.production * genome.getSpeed())); |
| 123 | + lines.add("gtbm.bee.label.flowering\t" + |
| 124 | + String.format("%.0f%%", modifiers.flowering * genome.getFlowering())); |
| 125 | + lines.add("gtbm.bee.label.lifespan\t" + |
| 126 | + String.format("%.0f%%", 100F * modifiers.lifespan * genome.getLifespan())); |
| 127 | + var t = genome.getTerritory(); |
| 128 | + lines.add("gtbm.bee.label.territory\t" + String.format("%.0f x %.0f x %.0f", |
| 129 | + t.getX() * modifiers.territory, t.getY() * modifiers.territory, |
| 130 | + t.getZ() * modifiers.territory)); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return lines; |
| 135 | + } |
| 136 | + |
| 137 | + // ---- Client-side: receive ---- |
| 138 | + |
| 139 | + @Override |
| 140 | + public void readUpdateInfo(int id, PacketBuffer buf) { |
| 141 | + if (id == SYNC_ERRORS) { |
| 142 | + int count = buf.readVarInt(); |
| 143 | + clientErrorIds = new ArrayList<>(); |
| 144 | + for (int i = 0; i < count; i++) { |
| 145 | + clientErrorIds.add(buf.readShort()); |
| 146 | + } |
| 147 | + } else if (id == SYNC_TOOLTIP) { |
| 148 | + int count = buf.readVarInt(); |
| 149 | + clientTooltip = new ArrayList<>(); |
| 150 | + for (int i = 0; i < count; i++) { |
| 151 | + clientTooltip.add(buf.readString(512)); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // ---- Client-side: render icon ---- |
| 157 | + |
| 158 | + @Override |
| 159 | + @SideOnly(Side.CLIENT) |
| 160 | + public void drawInBackground(int mouseX, int mouseY, float partialTicks, IRenderContext context) { |
| 161 | + if (clientErrorIds.isEmpty()) return; |
| 162 | + |
| 163 | + Position pos = getPosition(); |
| 164 | + Minecraft mc = Minecraft.getMinecraft(); |
| 165 | + mc.getTextureManager().bindTexture(ForestryAPI.textureManager.getGuiTextureMap()); |
| 166 | + GlStateManager.color(1, 1, 1, 1); |
| 167 | + GlStateManager.enableBlend(); |
| 168 | + GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); |
| 169 | + |
| 170 | + // Cycle through error icons (same as Gendustry: 40 tick interval) |
| 171 | + int index = (int) ((mc.world.getTotalWorldTime() / 40) % clientErrorIds.size()); |
| 172 | + short errorId = clientErrorIds.get(index); |
| 173 | + IErrorState errorState = ForestryAPI.errorStateRegistry.getErrorState(errorId); |
| 174 | + if (errorState != null) { |
| 175 | + TextureAtlasSprite sprite = errorState.getSprite(); |
| 176 | + if (sprite != null) { |
| 177 | + drawAtlasSprite(pos.x, pos.y, 16, 16, sprite); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + @SideOnly(Side.CLIENT) |
| 183 | + private static void drawAtlasSprite(int x, int y, int width, int height, TextureAtlasSprite sprite) { |
| 184 | + Tessellator tessellator = Tessellator.getInstance(); |
| 185 | + BufferBuilder buffer = tessellator.getBuffer(); |
| 186 | + buffer.begin(7, DefaultVertexFormats.POSITION_TEX); |
| 187 | + buffer.pos(x, y + height, 0).tex(sprite.getMinU(), sprite.getMaxV()).endVertex(); |
| 188 | + buffer.pos(x + width, y + height, 0).tex(sprite.getMaxU(), sprite.getMaxV()).endVertex(); |
| 189 | + buffer.pos(x + width, y, 0).tex(sprite.getMaxU(), sprite.getMinV()).endVertex(); |
| 190 | + buffer.pos(x, y, 0).tex(sprite.getMinU(), sprite.getMinV()).endVertex(); |
| 191 | + tessellator.draw(); |
| 192 | + } |
| 193 | + |
| 194 | + // ---- Client-side: render tooltip ---- |
| 195 | + |
| 196 | + @Override |
| 197 | + @SideOnly(Side.CLIENT) |
| 198 | + public void drawInForeground(int mouseX, int mouseY) { |
| 199 | + if (!isMouseOverElement(mouseX, mouseY)) return; |
| 200 | + if (clientTooltip.isEmpty()) return; |
| 201 | + |
| 202 | + List<String> tooltip = new ArrayList<>(); |
| 203 | + for (String line : clientTooltip) { |
| 204 | + // Lines with \t are "key\tvalue" pairs for localization |
| 205 | + int tab = line.indexOf('\t'); |
| 206 | + if (tab >= 0) { |
| 207 | + String key = line.substring(0, tab); |
| 208 | + String value = line.substring(tab + 1); |
| 209 | + // Temperature/humidity values need for.gui.* localization |
| 210 | + if (key.contains("temperature") || key.contains("humidity")) { |
| 211 | + tooltip.add(I18n.format(key, I18n.format("for.gui." + value))); |
| 212 | + } else { |
| 213 | + tooltip.add(I18n.format(key, value)); |
| 214 | + } |
| 215 | + } else { |
| 216 | + // Error lines (already have TextFormatting.RED + unlocalized key) |
| 217 | + if (line.startsWith(TextFormatting.RED.toString())) { |
| 218 | + String errorKey = line.substring(TextFormatting.RED.toString().length()); |
| 219 | + tooltip.add(TextFormatting.RED + I18n.format(errorKey)); |
| 220 | + } else { |
| 221 | + tooltip.add(line); |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + if (!tooltip.isEmpty()) { |
| 227 | + drawHoveringText(ItemStack.EMPTY, tooltip, 200, mouseX, mouseY); |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments