-
Notifications
You must be signed in to change notification settings - Fork 51
Fix block breaking bug #511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,26 +1,22 @@ | ||
| namespace Obsidian.API.Events; | ||
|
|
||
| public abstract class BlockEventArgs : BaseMinecraftEventArgs | ||
| public abstract class BlockEventArgs(IServer server, IBlock block, Vector location, IWorld world) : BaseMinecraftEventArgs(server) | ||
| { | ||
| /// <summary> | ||
| /// The impacted block. | ||
| /// </summary> | ||
| public IBlock Block { get; } | ||
| public IBlock Block { get; } = block; | ||
|
|
||
| /// <summary> | ||
| /// Location of the impacted block. | ||
| /// </summary> | ||
| public Vector Location { get; } | ||
| public Vector Location { get; } = location; | ||
|
|
||
| /// <summary> | ||
| /// World where the event took place. | ||
| /// </summary> | ||
| public IWorld World { get; } | ||
| public IWorld World { get; } = world; | ||
|
|
||
| protected BlockEventArgs(IServer server, IBlock block, Vector location) : base(server) | ||
| { | ||
| Block = block; | ||
| Location = location; | ||
| World = server.DefaultWorld; | ||
| } | ||
|
|
||
| public int Sequence { get; init; } | ||
| } |
This file contains hidden or 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,14 +1,9 @@ | ||
| namespace Obsidian.API.Events; | ||
|
|
||
| public class PlayerLeaveEventArgs : PlayerEventArgs | ||
| public class PlayerLeaveEventArgs(IPlayer player, IServer server, DateTimeOffset leave) : PlayerEventArgs(player, server) | ||
| { | ||
| /// <summary> | ||
| /// The date the player left. | ||
| /// </summary> | ||
| public DateTimeOffset LeaveDate { get; } | ||
|
|
||
| public PlayerLeaveEventArgs(IPlayer player, IServer server, DateTimeOffset leave) : base(player, server) | ||
| { | ||
| this.LeaveDate = leave; | ||
| } | ||
| public DateTimeOffset LeaveDate { get; } = leave; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
| using Obsidian.API.Events; | ||
| using Obsidian.Entities; | ||
| using Obsidian.Net.Packets.Play.Clientbound; | ||
|
|
||
| namespace Obsidian.Events; | ||
|
|
||
| public partial class MainEventHandler | ||
| { | ||
| [EventPriority(Priority = Priority.Internal)] | ||
| public async ValueTask OnBlockBreak(BlockBreakEventArgs args) | ||
| { | ||
| var player = args.Player; | ||
| var sequence = args.Sequence; | ||
| var block = args.Block; | ||
| var world = player.World; | ||
| var location = args.Location; | ||
|
|
||
| player.Client.SendPacket(new BlockChangedAckPacket | ||
| { | ||
| SequenceID = sequence | ||
| }); | ||
|
|
||
| if (args.IsCancelled) | ||
| { | ||
| player.Client.SendPacket(new BlockUpdatePacket(location, block.GetHashCode())); | ||
| return; | ||
| } | ||
|
|
||
| await world.SetBlockAsync(location, BlocksRegistry.Air, true); | ||
|
|
||
| player.Client.SendPacket(new BlockUpdatePacket(location, BlocksRegistry.Air.GetHashCode())); | ||
|
|
||
| world.PacketBroadcaster.QueuePacketToWorld(world, 0, new BlockDestructionPacket | ||
| { | ||
| EntityId = player.EntityId, | ||
| Position = location, | ||
| DestroyStage = -1 | ||
| }, player.EntityId); | ||
|
|
||
| var droppedItem = ItemsRegistry.GetSingleItem(block.Material); | ||
|
|
||
| if (droppedItem.Type == Material.Air) | ||
| return; | ||
|
|
||
| var item = new ItemEntity | ||
| { | ||
| EntityId = Server.GetNextEntityId(), | ||
| Item = droppedItem, | ||
| World = player.World, | ||
| Position = (VectorF)location + 0.5f, | ||
| }; | ||
|
|
||
| player.World.TryAddEntity(item); | ||
|
|
||
| var power = GetRandDropVelocity(); | ||
| var direction = Globals.Random.NextFloat() * 6.2f; | ||
|
|
||
| item.SpawnEntity(new Velocity(-Math.Sin(direction) * power, 0.2f, Math.Cos(direction) * power)); | ||
| } | ||
|
|
||
| private static float GetRandDropVelocity() | ||
| { | ||
| var f = Globals.Random.NextFloat(); | ||
|
|
||
| return f * 0.5f; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.