Skip to content

Commit 63702d1

Browse files
committed
new item events
1 parent fae9abb commit 63702d1

File tree

5 files changed

+155
-87
lines changed

5 files changed

+155
-87
lines changed

.github/workflows/dotnet.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ jobs:
3838
file: src/MiNET/MiNET.Console/minet.zip
3939
asset_name: MiNET.zip
4040
tag: ${{ github.ref }}
41-
release_name: MiNET-CobwebSMP 1.13.0.18 (1.21.30)
41+
release_name: MiNET-CobwebSMP 1.13.0.20 (1.21.30)
4242
body: ${{ github.event.head_commit.message }}

src/MiNET/MiNET.Console/minet.zip

17.1 KB
Binary file not shown.

src/MiNET/MiNET/ItemStackInventoryManager.cs

Lines changed: 94 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,23 @@ protected virtual void ProcessSwapAction(SwapAction action, List<StackResponseCo
266266
Item sourceItem = GetContainerItem(source.ContainerId, source.Slot);
267267
Item destItem = GetContainerItem(destination.ContainerId, destination.Slot);
268268

269-
SetContainerItem(source.ContainerId, source.Slot, destItem);
270-
SetContainerItem(destination.ContainerId, destination.Slot, sourceItem);
271269

272-
if (source.ContainerId == 21 || source.ContainerId == 22 || destination.ContainerId == 21 || destination.ContainerId == 22)
270+
if (!_player.OnItemTransaction(new ItemTransactionEventArgs(_player, _player.Level, sourceItem, action)))
273271
{
274-
if (!(GetContainerItem(21, 14) is ItemAir) && !(GetContainerItem(22, 15) is ItemAir)) Enchantment.SendEnchantments(_player, GetContainerItem(21, 14));
275-
else Enchantment.SendEmptyEnchantments(_player);
272+
//revert. Empty looks ok
273+
}
274+
else
275+
{
276+
SetContainerItem(source.ContainerId, source.Slot, destItem);
277+
SetContainerItem(destination.ContainerId, destination.Slot, sourceItem);
278+
279+
if (source.ContainerId == 21 || source.ContainerId == 22 || destination.ContainerId == 21 || destination.ContainerId == 22)
280+
{
281+
if (!(GetContainerItem(21, 14) is ItemAir) && !(GetContainerItem(22, 15) is ItemAir))
282+
Enchantment.SendEnchantments(_player, GetContainerItem(21, 14));
283+
else
284+
Enchantment.SendEmptyEnchantments(_player);
285+
}
276286
}
277287

278288
stackResponses.Add(new StackResponseContainerInfo
@@ -309,61 +319,68 @@ protected virtual StackRequestSlotInfo ProcessPlaceAction(PlaceAction action, Li
309319
{
310320
byte count = action.Count;
311321
Item sourceItem;
312-
Item destItem;
322+
Item destItem = new ItemAir();
313323
StackRequestSlotInfo source = action.Source;
314324
StackRequestSlotInfo destination = action.Destination;
315325

316326
sourceItem = GetContainerItem(source.ContainerId, source.Slot);
317327

318-
if (sourceItem.Count == count || sourceItem.Count - count <= 0)
319-
{
320-
destItem = sourceItem;
321-
sourceItem = new ItemAir();
322-
sourceItem.UniqueId = 0;
323-
SetContainerItem(source.ContainerId, source.Slot, sourceItem);
324-
}
325-
else
326-
{
327-
destItem = (Item) sourceItem.Clone();
328-
sourceItem.Count -= count;
329-
destItem.Count = count;
330-
destItem.UniqueId = Environment.TickCount;
331-
}
332-
333-
Item existingItem = GetContainerItem(destination.ContainerId, destination.Slot);
334-
if (existingItem.UniqueId > 0) // is empty/air is what this means
328+
if (!_player.OnItemTransaction(new ItemTransactionEventArgs(_player, _player.Level, sourceItem, action)))
335329
{
336-
existingItem.Count += count;
337-
destItem = existingItem;
330+
//revert. Empty looks ok
338331
}
339332
else
340333
{
341-
SetContainerItem(destination.ContainerId, destination.Slot, destItem);
342-
}
343-
if (destination.ContainerId == 6 || source.ContainerId == 6)
344-
{
345-
_player.SendArmorForPlayer(_player.Level.GetSpawnedPlayers());
346-
}
347-
else if (destination.ContainerId == 22)
348-
{
349-
if (Enum.IsDefined(typeof(ItemType), GetContainerItem(21, 14).ItemType) && !(GetContainerItem(22, 15) is ItemAir))
334+
if (sourceItem.Count == count || sourceItem.Count - count <= 0)
350335
{
351-
Enchantment.SendEnchantments(_player, GetContainerItem(21, 14));
336+
destItem = sourceItem;
337+
sourceItem = new ItemAir();
338+
sourceItem.UniqueId = 0;
339+
SetContainerItem(source.ContainerId, source.Slot, sourceItem);
352340
}
353341
else
354342
{
355-
Enchantment.SendEmptyEnchantments(_player);
343+
destItem = (Item) sourceItem.Clone();
344+
sourceItem.Count -= count;
345+
destItem.Count = count;
346+
destItem.UniqueId = Environment.TickCount;
356347
}
357-
}
358-
else if (destination.ContainerId == 23)
359-
{
360-
if (Enum.IsDefined(typeof(ItemType), GetContainerItem(21, 14).ItemType) && !(GetContainerItem(21, 14) is ItemAir) && !(GetContainerItem(22, 15) is ItemAir))
348+
349+
Item existingItem = GetContainerItem(destination.ContainerId, destination.Slot);
350+
if (existingItem.UniqueId > 0) // is empty/air is what this means
361351
{
362-
Enchantment.SendEnchantments(_player, GetContainerItem(21, 14));
352+
existingItem.Count += count;
353+
destItem = existingItem;
363354
}
364355
else
365356
{
366-
Enchantment.SendEmptyEnchantments(_player);
357+
SetContainerItem(destination.ContainerId, destination.Slot, destItem);
358+
}
359+
if (destination.ContainerId == 6 || source.ContainerId == 6)
360+
{
361+
_player.SendArmorForPlayer(_player.Level.GetSpawnedPlayers());
362+
}
363+
else if (destination.ContainerId == 22)
364+
{
365+
if (Enum.IsDefined(typeof(ItemType), GetContainerItem(21, 14).ItemType) && !(GetContainerItem(22, 15) is ItemAir))
366+
{
367+
Enchantment.SendEnchantments(_player, GetContainerItem(21, 14));
368+
}
369+
else
370+
{
371+
Enchantment.SendEmptyEnchantments(_player);
372+
}
373+
}
374+
else if (destination.ContainerId == 23)
375+
{
376+
if (Enum.IsDefined(typeof(ItemType), GetContainerItem(21, 14).ItemType) && !(GetContainerItem(21, 14) is ItemAir) && !(GetContainerItem(22, 15) is ItemAir))
377+
{
378+
Enchantment.SendEnchantments(_player, GetContainerItem(21, 14));
379+
}
380+
else
381+
{
382+
Enchantment.SendEmptyEnchantments(_player);
383+
}
367384
}
368385
}
369386

@@ -403,48 +420,54 @@ protected virtual void ProcessTakeAction(TakeAction action, List<StackResponseCo
403420
byte count = action.Count;
404421
Item sourceItem;
405422
Item destinationItem;
406-
Item destItem;
423+
Item destItem = new ItemAir();
407424
StackRequestSlotInfo source = action.Source;
408425
StackRequestSlotInfo destination = action.Destination;
409426

410427
sourceItem = GetContainerItem(source.ContainerId, source.Slot);
411428
destinationItem = GetContainerItem(destination.ContainerId, destination.Slot);
412429

413-
414-
if (source.ContainerId == 60 && sourceItem.Id == destinationItem.Id)
415-
{
416-
destItem = (Item) destinationItem.Clone();
417-
destItem.Count += count;
418-
destItem.UniqueId = Environment.TickCount;
419-
}
420-
else if (source.ContainerId != 60 && sourceItem.Count == count)
430+
if (!_player.OnItemTransaction(new ItemTransactionEventArgs(_player, _player.Level, sourceItem, action)))
421431
{
422-
destItem = (Item) sourceItem.Clone();
423-
destItem.Count = (byte)(destinationItem.Count + count);
424-
destItem.UniqueId = Environment.TickCount;
425-
sourceItem = new ItemAir();
426-
sourceItem.UniqueId = 0;
427-
SetContainerItem(source.ContainerId, source.Slot, sourceItem);
432+
//revert. Empty looks ok
428433
}
429434
else
430435
{
431-
destItem = (Item) sourceItem.Clone();
432-
destItem.Count = count;
433-
destItem.UniqueId = Environment.TickCount;
434-
sourceItem.Count -= count;
435-
SetContainerItem(source.ContainerId, source.Slot, sourceItem);
436-
}
436+
if (source.ContainerId == 60 && sourceItem.Id == destinationItem.Id)
437+
{
438+
destItem = (Item) destinationItem.Clone();
439+
destItem.Count += count;
440+
destItem.UniqueId = Environment.TickCount;
441+
}
442+
else if (source.ContainerId != 60 && sourceItem.Count == count)
443+
{
444+
destItem = (Item) sourceItem.Clone();
445+
destItem.Count = (byte) (destinationItem.Count + count);
446+
destItem.UniqueId = Environment.TickCount;
447+
sourceItem = new ItemAir();
448+
sourceItem.UniqueId = 0;
449+
SetContainerItem(source.ContainerId, source.Slot, sourceItem);
450+
}
451+
else
452+
{
453+
destItem = (Item) sourceItem.Clone();
454+
destItem.Count = count;
455+
destItem.UniqueId = Environment.TickCount;
456+
sourceItem.Count -= count;
457+
SetContainerItem(source.ContainerId, source.Slot, sourceItem);
458+
}
437459

438-
SetContainerItem(destination.ContainerId, destination.Slot, destItem);
460+
SetContainerItem(destination.ContainerId, destination.Slot, destItem);
439461

440-
if (source.ContainerId == 22 || source.ContainerId == 23)
441-
{
442-
Enchantment.SendEmptyEnchantments(_player);
443-
}
462+
if (source.ContainerId == 22 || source.ContainerId == 23)
463+
{
464+
Enchantment.SendEmptyEnchantments(_player);
465+
}
444466

445-
if (source.ContainerId == 6)
446-
{
447-
_player.SendArmorForPlayer(_player.Level.GetSpawnedPlayers());
467+
if (source.ContainerId == 6)
468+
{
469+
_player.SendArmorForPlayer(_player.Level.GetSpawnedPlayers());
470+
}
448471
}
449472

450473
stackResponses.Add(new StackResponseContainerInfo

src/MiNET/MiNET/MiNET.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
44
<PackageId>MiNET-CobwebSMP</PackageId>
5-
<Version>1.13.0.18</Version>
5+
<Version>1.13.0.20</Version>
66
<Authors>gurun</Authors>
77
<Company>Niclas Olofsson</Company>
88
<Description>MiNET - a Minecraft PocketEdition Server</Description>

src/MiNET/MiNET/Player.cs

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3072,25 +3072,32 @@ protected virtual void HandleTransactionRecords(List<TransactionRecord> records)
30723072
// Drop
30733073
Item sourceItem = Inventory.GetItemInHand();
30743074

3075-
if (newItem.Id != sourceItem.Id) Log.Warn($"Inventory mismatch. Client reported drop item as {newItem} and it did not match existing item {sourceItem}");
3076-
3077-
byte count = newItem.Count;
3078-
3079-
Item dropItem;
3080-
if (sourceItem.Count == count)
3075+
if (!OnItemDrop(new ItemDropEventArgs(this, Level, sourceItem)))
30813076
{
3082-
dropItem = sourceItem;
3083-
Inventory.ClearInventorySlot((byte) Inventory.InHandSlot);
3077+
Inventory.SendSetSlot(Inventory.InHandSlot);
30843078
}
30853079
else
30863080
{
3087-
dropItem = (Item) sourceItem.Clone();
3088-
sourceItem.Count -= count;
3089-
dropItem.Count = count;
3090-
dropItem.UniqueId = Environment.TickCount;
3091-
}
3081+
if (newItem.Id != sourceItem.Id) { Log.Warn($"Inventory mismatch. Client reported drop item as {newItem} and it did not match existing item {sourceItem}"); }
30923082

3093-
DropItem(dropItem);
3083+
byte count = newItem.Count;
3084+
3085+
Item dropItem;
3086+
if (sourceItem.Count == count)
3087+
{
3088+
dropItem = sourceItem;
3089+
Inventory.ClearInventorySlot((byte) Inventory.InHandSlot);
3090+
}
3091+
else
3092+
{
3093+
dropItem = (Item) sourceItem.Clone();
3094+
sourceItem.Count -= count;
3095+
dropItem.Count = count;
3096+
dropItem.UniqueId = Environment.TickCount;
3097+
}
3098+
3099+
DropItem(dropItem);
3100+
}
30943101
break;
30953102
}
30963103
}
@@ -4374,6 +4381,22 @@ protected virtual bool OnPlayerDamageToEntity(PlayerDamageToEntityEventArgs e)
43744381
return !e.Cancel;
43754382
}
43764383

4384+
public event EventHandler<ItemDropEventArgs> ItemDrop;
4385+
4386+
protected virtual bool OnItemDrop(ItemDropEventArgs e)
4387+
{
4388+
ItemDrop?.Invoke(this, e);
4389+
return !e.Cancel;
4390+
}
4391+
4392+
public event EventHandler<ItemTransactionEventArgs> ItemTransaction;
4393+
4394+
public virtual bool OnItemTransaction(ItemTransactionEventArgs e)
4395+
{
4396+
ItemTransaction?.Invoke(this, e);
4397+
return !e.Cancel;
4398+
}
4399+
43774400
public virtual void HandleMcpeNetworkStackLatency(McpeNetworkStackLatency message)
43784401
{
43794402
var packet = McpeNetworkStackLatency.CreateObject();
@@ -4435,4 +4458,26 @@ public PlayerDamageToEntityEventArgs(Entity entity, Player damager) : base(damag
44354458
Level = entity?.Level;
44364459
}
44374460
}
4461+
4462+
public class ItemDropEventArgs : LevelCancelEventArgs
4463+
{
4464+
public Item Item { get; }
4465+
4466+
public ItemDropEventArgs(Player player, Level level, Item item) : base(player, level)
4467+
{
4468+
Item = item;
4469+
}
4470+
}
4471+
4472+
public class ItemTransactionEventArgs : LevelCancelEventArgs
4473+
{
4474+
public Item Item { get; }
4475+
public ItemStackAction Action { get; }
4476+
4477+
public ItemTransactionEventArgs(Player player, Level level, Item item, ItemStackAction action) : base(player, level)
4478+
{
4479+
Item = item;
4480+
Action = action;
4481+
}
4482+
}
44384483
}

0 commit comments

Comments
 (0)