Commit 614f6b92 by Jonathan Thomas

Fixed consuming items when right-clicking on chat bubbles (with something in your hand)

parent 9a25ad12
Pipeline #12722 passed with stages
in 2 minutes 12 seconds
...@@ -21,6 +21,7 @@ All notable changes to **CreatureChat** are documented in this file. The format ...@@ -21,6 +21,7 @@ All notable changes to **CreatureChat** are documented in this file. The format
### Fixed ### Fixed
- Entity persistence is now fixed (after creating a character sheet). No more despawning mobs. - Entity persistence is now fixed (after creating a character sheet). No more despawning mobs.
- Fixed consuming items when right-clicking on chat bubbles (with something in your hand)
- Fixed crash when PROTECT behavior targets another player - Fixed crash when PROTECT behavior targets another player
- Fixed error when saving chat data while generating a new chat message - Fixed error when saving chat data while generating a new chat message
......
...@@ -4,12 +4,17 @@ import com.owlmaddie.chat.ChatDataManager; ...@@ -4,12 +4,17 @@ import com.owlmaddie.chat.ChatDataManager;
import com.owlmaddie.network.ClientPackets; import com.owlmaddie.network.ClientPackets;
import com.owlmaddie.utils.ClientEntityFinder; import com.owlmaddie.utils.ClientEntityFinder;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.render.Camera; import net.minecraft.client.render.Camera;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.particle.ParticleTypes; import net.minecraft.particle.ParticleTypes;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -30,24 +35,44 @@ public class ClickHandler { ...@@ -30,24 +35,44 @@ public class ClickHandler {
private static boolean wasClicked = false; private static boolean wasClicked = false;
public static void register() { public static void register() {
UseItemCallback.EVENT.register(ClickHandler::handleUseItemAction);
// Handle empty hand right-click
ClientTickEvents.END_CLIENT_TICK.register(client -> { ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (client.options.useKey.isPressed()) { if (client.options.useKey.isPressed()) {
if (!wasClicked) { if (!wasClicked && client.player != null && client.player.getMainHandStack().isEmpty()) {
// The key has just been pressed down, so handle the 'click' if (handleUseKeyClick(client)) {
handleUseKeyClick(client);
wasClicked = true; wasClicked = true;
} }
}
} else { } else {
// The key has been released, so reset the wasClicked flag
wasClicked = false; wasClicked = false;
} }
}); });
} }
public static void handleUseKeyClick(MinecraftClient client) { // Handle use-item right-click (non-empty hand)
private static TypedActionResult<ItemStack> handleUseItemAction(PlayerEntity player, World world, Hand hand) {
if (shouldCancelAction(world)) {
return TypedActionResult.fail(player.getStackInHand(hand));
}
return TypedActionResult.pass(player.getStackInHand(hand));
}
private static boolean shouldCancelAction(World world) {
if (world.isClient) {
MinecraftClient client = MinecraftClient.getInstance();
if (client != null && client.options.useKey.isPressed()) {
return handleUseKeyClick(client);
}
}
return false;
}
public static boolean handleUseKeyClick(MinecraftClient client) {
Camera camera = client.gameRenderer.getCamera(); Camera camera = client.gameRenderer.getCamera();
Entity cameraEntity = camera.getFocusedEntity(); Entity cameraEntity = camera.getFocusedEntity();
if (cameraEntity == null) return; if (cameraEntity == null) return false;
// Get the player from the client // Get the player from the client
ClientPlayerEntity player = client.player; ClientPlayerEntity player = client.player;
...@@ -122,9 +147,10 @@ public class ClickHandler { ...@@ -122,9 +147,10 @@ public class ClickHandler {
// Show chat // Show chat
ClientPackets.setChatStatus(closestEntity, ChatDataManager.ChatStatus.DISPLAY); ClientPackets.setChatStatus(closestEntity, ChatDataManager.ChatStatus.DISPLAY);
} }
return true;
} }
} }
return false;
} }
public static Vec3d[] getBillboardCorners(Vec3d center, Vec3d cameraPos, double height, double width, double yaw, double pitch) { public static Vec3d[] getBillboardCorners(Vec3d center, Vec3d cameraPos, double height, double width, double yaw, double pitch) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment