Skip to content

Commit 0c626e8

Browse files
committed
Map optimisation
1 parent 6fbf4f0 commit 0c626e8

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

src/MiNET/MiNET/Entities/ImageProviders/MapImageProvider.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#endregion
2525

26+
using System;
2627
using MiNET.Net;
2728
using MiNET.Utils;
2829

@@ -82,17 +83,19 @@ public virtual McpeWrapper GetBatch(MapInfo mapInfo, bool forced)
8283

8384
private byte[] GenerateColors(MapInfo map)
8485
{
85-
byte[] bytes = new byte[map.Col * map.Row * 4];
86+
Random random = new Random();
8687

88+
byte[] bytes = new byte[map.Col * map.Row * 4];
8789
int i = 0;
90+
8891
for (byte y = 0; y < map.Col; y++)
8992
{
9093
for (byte x = 0; x < map.Row; x++)
9194
{
92-
bytes[i++] = 255; // R
93-
bytes[i++] = 0; // G
94-
bytes[i++] = 0; // B
95-
bytes[i++] = 0xff; // A
95+
bytes[i++] = (byte) random.Next(256);
96+
bytes[i++] = (byte) random.Next(256);
97+
bytes[i++] = (byte) random.Next(256);
98+
bytes[i++] = 0xff;
9699
}
97100
}
98101

src/MiNET/MiNET/Entities/Passive/Villager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ namespace MiNET.Entities.Passive
3131
{
3232
public class Villager : PassiveMob
3333
{
34-
public int Variant { get; set; }
3534
public Villager(Level level) : base(EntityType.Villager, level)
3635
{
3736
Width = Length = 0.6;

src/MiNET/MiNET/Entities/World/MapEntity.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
#endregion
2525

26-
using System;
2726
using log4net;
2827
using MiNET.Entities.ImageProviders;
28+
using MiNET.Items;
2929
using MiNET.Net;
3030
using MiNET.Utils;
3131
using MiNET.Worlds;
@@ -37,7 +37,6 @@ public class MapEntity : Entity
3737
private static readonly ILog Log = LogManager.GetLogger(typeof(MapEntity));
3838

3939
public MapInfo MapInfo { get; set; }
40-
public bool initialized { get; set; } = false;
4140
public IMapImageProvider ImageProvider { get; set; }
4241

4342
public MapEntity(Level level, long mapId = EntityManager.EntityIdUndefined) : base(EntityType.None, level)
@@ -58,7 +57,7 @@ public MapEntity(Level level, long mapId = EntityManager.EntityIdUndefined) : ba
5857
var mapInfo = new MapInfo
5958
{
6059
MapId = EntityId,
61-
UpdateType = 0,
60+
UpdateType = 2,
6261
Scale = 0,
6362
X = 0,
6463
Z = 0,
@@ -96,15 +95,21 @@ public override void OnTick(Entity[] entities)
9695
{
9796
MapInfo.Data = ImageProvider.GetData(MapInfo, false);
9897
}
99-
100-
MapInfo.UpdateType = initialized ? (byte) 2 : (byte) 8;
10198

102-
var mapInfo = (MapInfo) MapInfo.Clone();
99+
MapInfo.UpdateType = 2;
100+
101+
foreach (Player player in Level.GetSpawnedPlayers())
102+
{
103+
if (player.Inventory.GetItemInHand() is ItemMap)
104+
{
105+
var mapInfo = (MapInfo) MapInfo.Clone();
106+
var msg = McpeClientboundMapItemData.CreateObject();
107+
108+
msg.mapinfo = mapInfo;
109+
player.SendPacket(msg);
110+
}
111+
}
103112

104-
var msg = McpeClientboundMapItemData.CreateObject();
105-
msg.mapinfo = mapInfo;
106-
Level.RelayBroadcast(msg);
107-
initialized = true;
108113
return;
109114
}
110115

src/MiNET/MiNET/PlayerInventory.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
using System;
2727
using System.Collections.Generic;
2828
using System.Linq;
29+
using fNbt;
2930
using log4net;
3031
using MiNET.Blocks;
3132
using MiNET.Entities;
33+
using MiNET.Entities.World;
3234
using MiNET.Items;
3335
using MiNET.Net;
3436
using MiNET.Utils;
@@ -292,6 +294,21 @@ public virtual void SetHeldItemSlot(int selectedHotbarSlot, bool sendToPlayer =
292294
{
293295
InHandSlot = selectedHotbarSlot;
294296

297+
if (GetItemInHand() is ItemMap)
298+
{
299+
long mapUuid = GetItemInHand().ExtraData.Get<NbtLong>("map_uuid").Value;
300+
if (Player.Level.TryGetEntity(mapUuid, out MapEntity mapEntity))
301+
{
302+
var mapInfo = mapEntity.MapInfo;
303+
mapInfo.UpdateType = 8;
304+
305+
var msg = McpeClientboundMapItemData.CreateObject();
306+
msg.mapinfo = mapInfo;
307+
308+
Player.SendPacket(msg);
309+
}
310+
}
311+
295312
if (sendToPlayer)
296313
{
297314
var order = McpeMobEquipment.CreateObject();

0 commit comments

Comments
 (0)