Jump to content

Recommended Posts

Posted

Hi!

I want to query player coordinates for my mod.

I use this code:

private async Task<List<object>> GetOnlinePlayersWithDetailsAsync()
{
    return await Task.Run(() =>
    {
        List<object> players = new List<object>();
        foreach (var player in api.World.AllOnlinePlayers)
        {
            var blockPos = player.Entity.Pos.AsBlockPos;
            var climate = api.World.BlockAccessor.GetClimateAt(blockPos, EnumGetClimateMode.NowValues);

            players.Add(new
            {
                Name = player.PlayerName,
                Temperature = climate.Temperature,
                Rainfall = climate.Rainfall,
                Coordinates = new
                {
                    X = blockPos.X,
                    Y = blockPos.Y,
                    Z = blockPos.Z
                }
            });
        }
        return players;
    });
}

 

The real player coordinates in game are 344, 135, 035

but i got: X: 51344, Y: 135, Z: 51035

It is somehow +51000, 0, +51000 

  • 2 weeks later...
Posted

 

first i query the spawn position:

 

  private object GetSpawnPoint()
  {
      try
      {
          var sp = api?.World?.DefaultSpawnPosition;
          if (sp == null) return null;

          var bp = sp.AsBlockPos;
          return new { X = bp.X, Y = bp.Y, Z = bp.Z };
      }
      catch
      {
          return null;
      }
  }

then the player position:

 

  private async Task<List<object>> GetOnlinePlayersWithDetailsAsync()
  {
      return await Task.Run(() =>
      {
          var players = new List<object>();

          var online = api?.World?.AllOnlinePlayers;
          if (online == null) return players;

          foreach (var player in online)
          {
              try
              {
                  if (player?.Entity == null) continue;

                  var pos = player.Entity.Pos?.XYZ ?? new Vec3d(0, api.World.SeaLevel, 0);
                  var blockPos = new BlockPos((int)pos.X, (int)pos.Y, (int)pos.Z);
                  var climate = api.World.BlockAccessor?.GetClimateAt(blockPos, EnumGetClimateMode.NowValues);
                  var watched = player.Entity.WatchedAttributes;

                  var playerData = new Dictionary<string, object>
                  {
                      ["Name"] = player.PlayerName,
                      ["Coordinates"] = new { X = blockPos.X, Y = blockPos.Y, Z = blockPos.Z }
                  };

                  if (config.ShowHealth)
                  {
                      var health = watched?.GetTreeAttribute("health");
                      playerData["Health"] = new
                      {
                          Current = FiniteOrNull(health?.GetFloat("currenthealth")),
                          Max = FiniteOrNull(health?.GetFloat("maxhealth"))
                      };
                  }

                  if (config.ShowHunger)
                  {
                      var hunger = watched?.GetTreeAttribute("hunger");
                      playerData["Hunger"] = new
                      {
                          Current = FiniteOrNull(hunger?.GetFloat("currentsaturation")),
                          Max = FiniteOrNull(hunger?.GetFloat("maxsaturation"))
                      };
                  }

                  if (config.ShowBodyTemp)
                  {
                      var bodyTemp = watched?.GetTreeAttribute("bodyTemp");
                      playerData["BodyTemp"] = FiniteOrNull(bodyTemp?.GetFloat("bodytemp"));
                  }

                  if (config.ShowWetness)
                  {
                      var wet = watched?.GetFloat("wetness", float.NaN);
                      playerData["Wetness"] = FiniteOrNull(wet);
                  }

                  if (config.ShowTemperatureForPlayers)
                  {
                      playerData["Temperature"] = FiniteOrNull(climate?.Temperature);
                      playerData["Rainfall"] = FiniteOrNull(climate?.Rainfall);
                  }

                  if (config.ShowEntitlementsForPlayers)
                  {
                      var entitlements = player.Entity?.Player?.Entitlements?
                          .Select(e => e?.Code)
                          .Where(code => !string.IsNullOrEmpty(code))
                          .ToList() ?? new List<string>();
                      playerData["Entitlements"] = entitlements;
                  }

                  players.Add(playerData);
              }
              catch (Exception ex)
              {
                  api?.Logger?.Warning("ServerStatusQueryMod player collect error: " + ex.Message);
              }
          }

          return players;
      });
  }

 

Then on my webserver i simply do a math: (ofc i can do it inside the mod not outside but it works this way now)

 function renderPlayer(player, spawnPoint) {
      const li = document.createElement("li");

      const entitlements = Array.isArray(player.Entitlements) && player.Entitlements.length > 0
        ? ` [${player.Entitlements.join(", ")}]` : "";
      const nameEl = document.createElement('div');
      nameEl.innerHTML = `🧍 <strong>${player.Name}${entitlements}</strong>`;
      li.appendChild(nameEl);

      const coordsEl = document.createElement('div');
      coordsEl.className = 'coords';
      const spawnX = spawnPoint?.X ?? 0;
      const spawnZ = spawnPoint?.Z ?? 0;
      coordsEl.textContent = `(X: ${(player.Coordinates.X - spawnX)}, Y: ${player.Coordinates.Y}, Z: ${(player.Coordinates.Z - spawnZ)})`;
      li.appendChild(coordsEl);


So my mod is now fully works https://mods.vintagestory.at/show/mod/27202

allatok_77af80ff28fd5702ca620bc11d0fd5e6.thumb.jpg.0cb6f313a818935e9cbc76aa25093f52.jpg

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.