func (rl *resourceList) redraw() { for _, s := range rl.packs { s.Hide() render.FreeIcon(s.id) } rl.packs = rl.packs[:0] os.MkdirAll("./resource-packs", 0777) files, _ := ioutil.ReadDir("./resource-packs") for i, f := range files { f := f if !strings.HasSuffix(f.Name(), ".zip") { continue } fullName := filepath.Join("./resource-packs", f.Name()) desc, iimg, ok := getPackInfo(fullName) if !ok { continue } sc := scene.New(true) container := ui.NewContainer(0, float64(i)*100, 700, 100). Attach(ui.Center, ui.Middle) r := make([]byte, 20) rand.Read(r) si := &resourceListItem{ Type: sc, container: container, offset: float64(i), id: "servericon:" + string(r), } si.updatePosition() rl.packs = append(rl.packs, si) var rr, gg, bb int if resource.IsActive(fullName) { rr = 200 gg = 200 } bck := ui.NewImage(render.GetTexture("solid"), 0, 0, 700, 100, 0, 0, 1, 1, rr, gg, bb).Attach(ui.Top, ui.Left) bck.SetA(100) bck.AttachTo(container) sc.AddDrawable(bck) txt := ui.NewText(f.Name(), 90+10, 5, 255, 255, 255).Attach(ui.Top, ui.Left) txt.AttachTo(container) sc.AddDrawable(txt) var tex render.TextureInfo if iimg == nil { tex = render.GetTexture("misc/unknown_pack") } else { render.AddIcon(si.id, iimg) tex = render.Icon(si.id) } icon := ui.NewImage(tex, 5, 5, 90, 90, 0, 0, 1, 1, 255, 255, 255). Attach(ui.Top, ui.Left) icon.AttachTo(container) sc.AddDrawable(icon) msg := format.Wrap(&format.TextComponent{Text: desc}) format.ConvertLegacy(msg) motd := ui.NewFormattedWidth(msg, 90+10, 5+18, 700-(90+10+5)).Attach(ui.Top, ui.Left) motd.AttachTo(container) sc.AddDrawable(motd) container.ClickFunc = func() { if resource.IsActive(fullName) { RemovePack(fullName) } else { AddPack(fullName) } setScreen(newResourceList(rl.ret)) } container.HoverFunc = func(over bool) { if over { bck.SetA(200) } else { bck.SetA(100) } } sc.AddDrawable(container) } }
func (sl *serverList) pingServer(addr string, motd, version *ui.Formatted, icon *ui.Image, id string, ping *ui.Image, players *ui.Text) { conn, err := protocol.Dial(addr) if err != nil { syncChan <- func() { msg := &format.TextComponent{Text: err.Error()} msg.Color = format.Red motd.Update(format.Wrap(msg)) } return } defer conn.Close() resp, pingTime, err := conn.RequestStatus() syncChan <- func() { if err != nil { msg := &format.TextComponent{Text: err.Error()} msg.Color = format.Red motd.Update(format.Wrap(msg)) return } y := 0.0 pt := pingTime.Seconds() / 1000 switch { case pt <= 75: y = 16 / 256.0 case pt <= 150: y = 24 / 256.0 case pt <= 225: y = 32 / 256.0 case pt <= 350: y = 40 / 256.0 case pt < 999: y = 48 / 256.0 default: y = 56 / 256.0 } ping.SetTextureY(y) if resp.Version.Protocol == protocol.SupportedProtocolVersion { players.SetG(255) players.SetB(255) players.Update(fmt.Sprintf("%d/%d", resp.Players.Online, resp.Players.Max)) } else { players.SetG(85) players.SetB(85) players.Update(fmt.Sprintf("Out of date %d/%d", resp.Players.Online, resp.Players.Max)) } txt := &format.TextComponent{Text: resp.Version.Name} txt.Color = format.Yellow ver := format.Wrap(txt) format.ConvertLegacy(ver) version.Update(ver) desc := resp.Description format.ConvertLegacy(desc) motd.Update(desc) if strings.HasPrefix(resp.Favicon, "data:image/png;base64,") { favicon := resp.Favicon[len("data:image/png;base64,"):] data, err := base64.StdEncoding.DecodeString(favicon) if err != nil { fmt.Printf("error base64 decoding favicon: %s\n", err) return } img, err := png.Decode(bytes.NewReader(data)) if err != nil { fmt.Printf("error decoding favicon: %s\n", err) return } render.AddIcon(id, img) icon.SetTexture(render.Icon(id)) } } }