hammy275 / immersive-mc

A Minecraft mod to make Minecraft more immersive!
GNU Lesser General Public License v3.0
63 stars 9 forks source link

Item Throwing #180

Closed hammy275 closed 1 year ago

hammy275 commented 1 year ago

See this thread on the ImmersiveMC Discord: https://discord.com/channels/1061675558347489343/1065409551324684288

hammy275 commented 1 year ago

Will only support primary hand throwing.

Looks like the way to do this is a mixin into Projectile#shootFromRotation.

We'll want to average the velocity of the hand every tick for the past 3 ticks and use that as our throwing velocity. Might move this to server-side at some future point, but for now, I'll run this client side. On release, we send a packet to the server containing the velocity we should throw at.

From there, the server will call use or releaseUsing (depending on the item) after adding the player to a global HashMap. Call it vrNextThrowData or something, with keys being player names or UUIDs, and values being the velocity from the packet.

In our Mixin, we check if the player is part of the aforementioned HashMap. If so, we use their hand's rotation. Additionally, at the end of shootFromRotation, we check again if they're in the HashMap. If so, we remove them from the map, and use the provided velocity to augment the delta movement of the projectile. This augmentation I'll have to experiment with until I find something that "feels right".

hammy275 commented 1 year ago

Based on https://minecraft-archive.fandom.com/wiki/Category:Projectiles , we'll support the following items:

Notable exceptions:

hammy275 commented 1 year ago

Also, since people may throw things in a variety of different ways, to determine the direction, we'll do the difference between the current controller pos and the last, then normalize it. To get it to a x and y rotation, we should be able to reverse the calculation done in Entity#calculateViewVector. Note that the constant being multiplied there is pi/180.

EDIT: Also, for throwable items, we always cancel their left click action with the exception of the Trident, which we only cancel if we're not hovering over an Entity at melee range.

hammy275 commented 1 year ago

For where I'm currently stuck, here are some ideas:

hammy275 commented 1 year ago

Entire pivot:

Instead of trying to override shootFromRotation as a whole (since MIxin config plugins can't affect other mods, it seems), instead, we can take advantage of @Redirect. For each individual item, whenever it calls shootFromRotation from its use method (or the other for Tridents), we call our own entire shoot method instead. If we personally don't run, pass control over to the normal shootFromRotation instead.

hammy275 commented 1 year ago

Part 1 of this is at https://github.com/hammy3502/immersive-mc/commit/9df56606c086c9bc44a684df2ff2538f56485abb .

Things left to do:

hammy275 commented 1 year ago

Trident rendering done (even comes with rumble!)

hammy275 commented 1 year ago

Velocity tuning is done! Next is a config entry, then we can port to other versions!

hammy275 commented 1 year ago

I don't even need to worry about not blocking it for the first tick; we run at the end of the game tick! :)

hammy275 commented 1 year ago

It already works identically in Optifine Vivecraft let's gooooo

hammy275 commented 1 year ago

This is finally done! Here's all the commits from oldest to newest: