Skip to content

Commit

Permalink
Merge pull request #26 from psu-de/feature/crafting
Browse files Browse the repository at this point in the history
Added crafting
  • Loading branch information
psu-de authored Dec 4, 2023
2 parents 49739ee + 3a1e474 commit 51bf752
Show file tree
Hide file tree
Showing 32 changed files with 552 additions and 348 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"[csharp]": {
"editor.defaultFormatter": "ms-dotnettools.csharp"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ public class CloseWindowPacket : IPacket
{
public PacketType Type => PacketType.CB_Play_CloseWindow;

public int WindowId { get; set; }
public byte WindowId { get; set; }

public CloseWindowPacket(int windowId)
public CloseWindowPacket(byte windowId)
{
this.WindowId = windowId;
}

public void Write(PacketBuffer buffer, MinecraftData version)
{
buffer.WriteVarInt(this.WindowId);
buffer.WriteByte(this.WindowId);
}

public static IPacket Read(PacketBuffer buffer, MinecraftData version)
{
return new CloseWindowPacket(
buffer.ReadVarInt());
buffer.ReadByte());
}
}
5 changes: 4 additions & 1 deletion Components/MineSharp.Protocol/Packets/PacketPalette.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
using SBFinishConfigurationPacket = MineSharp.Protocol.Packets.Serverbound.Configuration.FinishConfigurationPacket;
using CBFinishConfigurationPacket = MineSharp.Protocol.Packets.Clientbound.Configuration.FinishConfigurationPacket;
using SBPluginMessagePacket = MineSharp.Protocol.Packets.Serverbound.Configuration.PluginMessagePacket;
using CBCloseWindowPacket = MineSharp.Protocol.Packets.Clientbound.Play.CloseWindowPacket;
using SBCloseWindowPacket = MineSharp.Protocol.Packets.Serverbound.Play.CloseWindowPacket;

namespace MineSharp.Protocol.Packets;

Expand Down Expand Up @@ -135,7 +137,7 @@ private static void InitializePackets()
RegisterPacket<WindowItemsPacket>(PacketType.CB_Play_WindowItems);
RegisterPacket<WindowSetSlotPacket>(PacketType.CB_Play_SetSlot);
RegisterPacket<OpenWindowPacket>(PacketType.CB_Play_OpenWindow);
RegisterPacket<CloseWindowPacket>(PacketType.CB_Play_CloseWindow);
RegisterPacket<CBCloseWindowPacket>(PacketType.CB_Play_CloseWindow);
RegisterPacket<SetHeldItemPacket>(PacketType.CB_Play_HeldItemSlot);
RegisterPacket<SystemChatMessagePacket>(PacketType.CB_Play_SystemChat);
RegisterPacket<DisguisedChatMessagePacket>(PacketType.CB_Play_ProfilelessChat);
Expand All @@ -157,6 +159,7 @@ private static void InitializePackets()
RegisterPacket<PlayerActionPacket>(PacketType.SB_Play_BlockDig);
RegisterPacket<SwingArmPacket>(PacketType.SB_Play_ArmAnimation);
RegisterPacket<InteractPacket>(PacketType.SB_Play_UseItem);
RegisterPacket<SBCloseWindowPacket>(PacketType.SB_Play_CloseWindow);
}

private static void RegisterPacket<TPacket>(PacketType type) where TPacket : IPacket
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using MineSharp.Core.Common;
using MineSharp.Data;
using MineSharp.Data.Protocol;

namespace MineSharp.Protocol.Packets.Serverbound.Play;

public class CloseWindowPacket : IPacket
{
public PacketType Type => PacketType.SB_Play_CloseWindow;

public byte WindowId { get; set; }

public CloseWindowPacket(byte windowId)
{
this.WindowId = windowId;
}

public void Write(PacketBuffer buffer, MinecraftData version)
{
buffer.WriteByte(this.WindowId);
}

public static IPacket Read(PacketBuffer buffer, MinecraftData version)
{
return new CloseWindowPacket(
buffer.ReadByte());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void Write(PacketBuffer buffer, MinecraftData version)
buffer.WriteVarInt(this.StateId);
buffer.WriteShort(this.Slot);
buffer.WriteSByte(this.MouseButton);
buffer.WriteVarInt(this.MouseButton);
buffer.WriteVarInt(this.Mode);
buffer.WriteVarIntArray(this.ChangedSlots, (buff, slot) => buff.WriteSlot(slot));
buffer.WriteOptionalItem(this.SelectedItem);
}
Expand Down
226 changes: 122 additions & 104 deletions Components/MineSharp.Windows/Clicks/SimpleWindowClick.cs
Original file line number Diff line number Diff line change
@@ -1,130 +1,148 @@
using MineSharp.Core.Common;
using MineSharp.Core.Common.Items;

namespace MineSharp.Windows.Clicks;

internal class SimpleWindowClick : WindowClick
{
public override ClickMode ClickMode => ClickMode.SimpleClick;
{
public override ClickMode ClickMode => ClickMode.SimpleClick;

internal SimpleWindowClick(Window window, short slot, byte button) : base(window, slot, button)
{ }

private void PerformOutsideClick()
private IList<Slot> _changedSlots = new List<Slot>();

internal SimpleWindowClick(Window window, short slot, byte button) : base(window, slot, button)
{ }

private void PerformOutsideClick()
{
// Clicked outside, drop item stack
if (this.Window.GetSelectedSlot().IsEmpty())
{
// Clicked outside, drop item stack
if (this.Window.GetSelectedSlot().IsEmpty())
{
return;
}
return;
}

var selectedItem = this.Window.GetSelectedSlot();
if (this.Button == 0) // Drop entire stack
var selectedItem = this.Window.GetSelectedSlot();
if (this.Button == 0) // Drop entire stack
{
selectedItem.Item = null;
} else
{ // Drop one at a time
selectedItem.Item!.Count--;
if (selectedItem.Item!.Count == 0)
{
selectedItem.Item = null;
} else
{ // Drop one at a time
selectedItem.Item!.Count--;
if (selectedItem.Item!.Count == 0)
{
selectedItem.Item = null;
}
}
this.Window.SetSelectedSlot(selectedItem);
}

this.Window.UpdateSlot(selectedItem.Item, Window.SELECTED_SLOT);
}

private void PerformLeftClick()
{
// Swap selected slot and clicked slot
var selectedSlot = this.Window.GetSelectedSlot().Clone();
var clickedSlot = this.Window.GetSlot(this.Slot);
private void PerformLeftClick()
{
// Swap selected slot and clicked slot
var selectedSlot = this.Window.GetSelectedSlot();
var clickedSlot = this.Window.GetSlot(this.Slot);

if (selectedSlot.IsEmpty() && clickedSlot.IsEmpty())
return;
if (selectedSlot.IsEmpty() && clickedSlot.IsEmpty())
return;

if (selectedSlot.Item?.Info.Id == clickedSlot.Item?.Info.Id)
{
// stack items, both items cannot be null
int left = selectedSlot.Item!.Count - clickedSlot.LeftToStack;
if (left < 0)
left = 0;

clickedSlot.Item!.Count += (byte)(selectedSlot.Item!.Count - left);
selectedSlot.Item!.Count = (byte)left;
this.Window.SetSlot(clickedSlot);
this.Window.SetSlot(selectedSlot);
return;
}
this._changedSlots.Add(clickedSlot);

if (selectedSlot.Item?.Info.Id == clickedSlot.Item?.Info.Id)
{
// stack items, both items cannot be null
int left = selectedSlot.Item!.Count - clickedSlot.LeftToStack;
if (left < 0)
left = 0;

//swap items
clickedSlot.SlotIndex = -1;
this.Window.SetSlot(clickedSlot); // set selected slot
clickedSlot.Item!.Count += (byte)(selectedSlot.Item!.Count - left);
selectedSlot.Item!.Count = (byte)left;

selectedSlot.SlotIndex = this.Slot;
this.Window.SetSlot(selectedSlot);
if (selectedSlot.Item!.Count == 0)
selectedSlot.Item = null;

this.Window.UpdateSlot(clickedSlot.Item, this.Slot);
this.Window.UpdateSlot(selectedSlot.Item, Window.SELECTED_SLOT);
return;
}

//swap items
(clickedSlot.Item, selectedSlot.Item) = (selectedSlot.Item, clickedSlot.Item);
this.Window.UpdateSlot(clickedSlot.Item, this.Slot);
this.Window.UpdateSlot(selectedSlot.Item, Window.SELECTED_SLOT);
}

private void PerformRightClick()
{
if (this.Window.GetSelectedSlot().IsEmpty() && this.Window.GetSlot(this.Slot).IsEmpty())
return;

if (this.Window.GetSelectedSlot().IsEmpty())
{
// Pickup half stack
var oldSlot = this.Window.GetSlot(this.Slot);
var count = (byte)Math.Ceiling(oldSlot.Item!.Count / 2.0F);
var selectedSlot = this.Window.GetSelectedSlot();
selectedSlot.Item = oldSlot.Item.Clone();
selectedSlot.Item.Count = count;

oldSlot.Item.Count -= count;

this.Window.SetSlot(oldSlot);
return;
}
private void PerformRightClick()
{
var clickedSlot = this.Window.GetSlot(this.Slot);
var selectedSlot = this.Window.GetSelectedSlot();
if (selectedSlot.IsEmpty() && clickedSlot.IsEmpty())
return;

if (selectedSlot.IsEmpty())
{
// Pickup half stack
var count = (byte)Math.Ceiling(clickedSlot.Item!.Count / 2.0F);
var newSelectedItem = clickedSlot.Item.Clone();
newSelectedItem.Count = count;

clickedSlot.Item.Count -= count;
this._changedSlots.Add(clickedSlot);

this.Window.UpdateSlot(newSelectedItem, Window.SELECTED_SLOT);
this.Window.UpdateSlot(clickedSlot.Item, clickedSlot.SlotIndex);
return;
}

if (this.Window.GetSlot(this.Slot).IsEmpty() || this.Window.GetSlot(this.Slot).CanStack(this.Window.GetSelectedSlot()))
{
// Transfer one item from selectedSlot to slots[Slot]
var selectedSlot = this.Window.GetSelectedSlot();
selectedSlot.Item!.Count -= 1;

var clickedSlot = this.Window.GetSlot(this.Slot);

if (clickedSlot.IsEmpty())
{
clickedSlot.Item = selectedSlot.Item!.Clone();
clickedSlot.Item!.Count = 1;
} else
{
clickedSlot.Item!.Count += 1;
}

this.Window.SetSelectedSlot(selectedSlot);
this.Window.SetSlot(clickedSlot); // Clone Item?
if (clickedSlot.IsEmpty() || clickedSlot.CanStack(selectedSlot, 1))
{
// Transfer one item from selectedSlot to slots[Slot]
selectedSlot.Item!.Count -= 1;

if (clickedSlot.IsEmpty())
{
clickedSlot.Item = new Item(
selectedSlot.Item.Info,
1,
selectedSlot.Item.Damage,
selectedSlot.Item.Metadata); // TODO: Clone metadata?
} else
{
// just swap selected slot and clicked swap, like a left click
this.PerformLeftClick();
clickedSlot.Item!.Count += 1;
}
this.Window.UpdateSlot(selectedSlot.Item, Window.SELECTED_SLOT);
this.Window.UpdateSlot(clickedSlot.Item, this.Slot);
this._changedSlots.Add(clickedSlot);
} else
{
// just swap selected slot and clicked swap, like a left click
this.PerformLeftClick();
}
}

public override void PerformClick()
{
if (!(this.Button == 0 || this.Button == 1))
throw new NotSupportedException();
public override void PerformClick()
{
this._changedSlots.Clear();
if (!(this.Button == 0 || this.Button == 1))
throw new NotSupportedException();

if (this.Slot == OutsideClick)
{
this.PerformOutsideClick();
return;
}
if (this.Slot == OutsideClick)
{
this.PerformOutsideClick();
return;
}

if (this.Button == 0)
{
// Swap selected slot and clicked slot
this.PerformLeftClick();
return;
}

this.PerformRightClick();
if (this.Button == 0)
{
// Swap selected slot and clicked slot
this.PerformLeftClick();
return;
}
}

this.PerformRightClick();
}

public override Slot[] GetChangedSlots()
{
return this._changedSlots.ToArray();
}
}
4 changes: 4 additions & 0 deletions Components/MineSharp.Windows/Clicks/WindowClick.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using MineSharp.Core.Common;

namespace MineSharp.Windows.Clicks;

public abstract class WindowClick
Expand All @@ -16,6 +18,8 @@ protected WindowClick(Window window, short slot, byte button)
this.Slot = slot;
this.Button = button;
}

public abstract Slot[] GetChangedSlots();

/// <summary>
/// Performs the click on <see cref="Window"/>
Expand Down
Loading

0 comments on commit 51bf752

Please sign in to comment.