-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from psu-de/feature/crafting
Added crafting
- Loading branch information
Showing
32 changed files
with
552 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"[csharp]": { | ||
"editor.defaultFormatter": "ms-dotnettools.csharp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Components/MineSharp.Protocol/Packets/Serverbound/Play/CloseWindowPacket.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
226 changes: 122 additions & 104 deletions
226
Components/MineSharp.Windows/Clicks/SimpleWindowClick.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.