Skip to content

Commit

Permalink
Fixed issues with UseItem functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
GirafiStudios committed Dec 9, 2023
1 parent 57480b2 commit 5705da2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.girafi.minemenu.network.packet.server;

import commonnetwork.Constants;
import com.girafi.minemenu.Constants;
import commonnetwork.networking.data.PacketContext;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BucketItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;

public class PacketUseItem {
public static final ResourceLocation CHANNEL = new ResourceLocation(Constants.MOD_ID, "packet_use_item");
Expand All @@ -29,6 +37,7 @@ public static PacketUseItem decode(FriendlyByteBuf buf) {

public static void handle(PacketContext<PacketUseItem> ctx) {
ServerPlayer player = ctx.sender();

if (player != null) {
ItemStack slotStack = player.getInventory().getItem(ctx.message().slot);
ItemStack heldSaved = player.getMainHandItem();
Expand All @@ -37,17 +46,39 @@ public static void handle(PacketContext<PacketUseItem> ctx) {

player.setItemSlot(slot, slotStack);
ItemStack heldItem = player.getItemInHand(hand);
InteractionResultHolder<ItemStack> useStack = heldItem.use(player.level(), player, hand);
if (useStack.getResult() == InteractionResult.SUCCESS) {
player.getInventory().items.set(ctx.message().slot, useStack.getObject());

UseOnContext useOnContext = new UseOnContext(player, hand, getPlayerPOVHitResult(player.level(), player, ClipContext.Fluid.SOURCE_ONLY));
if (heldItem.useOn(useOnContext) == InteractionResult.PASS) { //useOn check here, also does the actual interaction
InteractionResult use = heldItem.use(player.level(), player, hand).getResult();
if (use.consumesAction()) {
if (heldItem.getItem() instanceof BucketItem) {
player.getInventory().items.set(ctx.message().slot, new ItemStack(Items.BUCKET));
}
}
}
player.setItemSlot(slot, heldSaved);

player.setItemSlot(slot, heldSaved); //Sets the heldStack to what it was before something was used, to prevent losing whatever the play have in their hand

player.inventoryMenu.sendAllDataToRemote();
}
}

private static EquipmentSlot getSlotFromHand(InteractionHand hand) {
public static BlockHitResult getPlayerPOVHitResult(Level p_41436_, Player p_41437_, ClipContext.Fluid p_41438_) {
float f = p_41437_.getXRot();
float f1 = p_41437_.getYRot();
Vec3 vec3 = p_41437_.getEyePosition();
float f2 = Mth.cos(-f1 * ((float) Math.PI / 180F) - (float) Math.PI);
float f3 = Mth.sin(-f1 * ((float) Math.PI / 180F) - (float) Math.PI);
float f4 = -Mth.cos(-f * ((float) Math.PI / 180F));
float f5 = Mth.sin(-f * ((float) Math.PI / 180F));
float f6 = f3 * f4;
float f7 = f2 * f4;
double d0 = 4.5D; //Workaround, since no getter for reach?
Vec3 vec31 = vec3.add((double) f6 * d0, (double) f5 * d0, (double) f7 * d0);
return p_41436_.clip(new ClipContext(vec3, vec31, ClipContext.Block.OUTLINE, p_41438_, p_41437_));
}

public static EquipmentSlot getSlotFromHand(InteractionHand hand) {
return hand == InteractionHand.MAIN_HAND ? EquipmentSlot.MAINHAND : EquipmentSlot.OFFHAND;
}
}
5 changes: 2 additions & 3 deletions common/src/main/resources/minemenu.accesswidener
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
accessWidener v1 named
accessible method net/minecraft/client/gui/screens/controls/KeyBindsList$Entry refreshEntry ()V
extendable method net/minecraft/client/gui/screens/controls/KeyBindsList$Entry refreshEntry ()V
accessible method net/minecraft/client/gui/components/AbstractButton renderWidget (Lnet/minecraft/client/gui/GuiGraphics;IIF)V
accessible field net/minecraft/client/KeyMapping key Lcom/mojang/blaze3d/platform/InputConstants$Key;
accessible field net/minecraft/client/gui/components/AbstractButton SPRITES Lnet/minecraft/client/gui/components/WidgetSprites;
accessible field net/minecraft/client/renderer/GameRenderer renderBuffers Lnet/minecraft/client/renderer/RenderBuffers;
accessible field net/minecraft/client/gui/components/AbstractButton SPRITES Lnet/minecraft/client/gui/components/WidgetSprites;
2 changes: 1 addition & 1 deletion fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies {

modImplementation "fuzs.forgeconfigapiport:forgeconfigapiport-fabric:${forge_config_api_port}"
modApi "mysticdrew:common-networking-fabric:${common_networking}-${minecraft_version}"
include "mysticdrew:common-networking-fabric:${common_networking}-${minecraft_version}"
modImplementation "mysticdrew:common-networking-fabric:${common_networking}-${minecraft_version}"
}

loom {
Expand Down

0 comments on commit 5705da2

Please sign in to comment.