Commit 53a0ba80 by Jonathan Thomas

- Submit button is now disabled by default. Must type text to submit

- Click Handler no longer plays sounds
- Moved sounds to ClientPackets, so all clients play nearby sounds from the Chat interactions. Sound gets quieter with distance (0 to 8 blocks)
parent 56459d1f
Pipeline #12009 passed with stage
in 20 seconds
......@@ -8,9 +8,12 @@ import com.owlmaddie.utils.ClientEntityFinder;
import com.owlmaddie.utils.Decompression;
import io.netty.buffer.Unpooled;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.sound.SoundEvents;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -82,7 +85,7 @@ public class ClientPackets {
ClientPlayNetworking.registerGlobalReceiver(ServerPackets.PACKET_S2C_MESSAGE, (client, handler, buffer, responseSender) -> {
// Read the data from the server packet
UUID entityId = UUID.fromString(buffer.readString());
String playerId = buffer.readString();
String playerIdStr = buffer.readString();
String message = buffer.readString(32767);
int line = buffer.readInt();
String status_name = buffer.readString(32767);
......@@ -95,7 +98,7 @@ public class ClientPackets {
if (entity != null) {
ChatDataManager chatDataManager = ChatDataManager.getClientInstance();
ChatDataManager.EntityChatData chatData = chatDataManager.getOrCreateChatData(entity.getUuidAsString());
chatData.playerId = playerId;
chatData.playerId = playerIdStr;
if (!message.isEmpty()) {
chatData.currentMessage = message;
}
......@@ -104,10 +107,13 @@ public class ClientPackets {
chatData.sender = ChatDataManager.ChatSender.valueOf(sender_name);
chatData.friendship = friendship;
if (chatData.sender == ChatDataManager.ChatSender.USER && !playerId.isEmpty()) {
if (chatData.sender == ChatDataManager.ChatSender.USER && !playerIdStr.isEmpty()) {
// Add player message to queue for rendering
PlayerMessageManager.addMessage(UUID.fromString(chatData.playerId), chatData.currentMessage, ChatDataManager.TICKS_TO_DISPLAY_USER_MESSAGE);
}
// Play sound with volume based on distance (from player or entity)
playNearbyUISound(client, entity, 0.2f);
}
});
});
......@@ -156,16 +162,32 @@ public class ClientPackets {
UUID playerId = UUID.fromString(buffer.readString());
boolean isChatOpen = buffer.readBoolean();
// Get player instance
PlayerEntity player = ClientEntityFinder.getPlayerEntityFromUUID(playerId);
// Update the player status data manager on the client-side
client.execute(() -> { // Make sure to run on the client thread
if (isChatOpen) {
PlayerMessageManager.openChatUI(playerId);
playNearbyUISound(client, player, 0.2f);
} else {
PlayerMessageManager.closeChatUI(playerId);
}
});
});
}
private static void playNearbyUISound(MinecraftClient client, Entity player, float maxVolume) {
// Play sound with volume based on distance
int distance_squared = 64;
if (client.player != null) {
double distance = client.player.squaredDistanceTo(player.getX(), player.getY(), player.getZ());
if (distance <= distance_squared) {
// Decrease volume based on distance
float volume = maxVolume - (float)distance / distance_squared * maxVolume;
client.player.playSound(SoundEvents.UI_BUTTON_CLICK.value(), volume, 0.8F);
}
}
}
}
......@@ -2,14 +2,12 @@ package com.owlmaddie.ui;
import com.owlmaddie.chat.ChatDataManager;
import com.owlmaddie.network.ClientPackets;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
......@@ -46,6 +44,7 @@ public class ChatScreen extends Screen {
textField.setMaxLength(ChatDataManager.MAX_CHAR_IN_USER_MESSAGE);
textField.setDrawsBackground(true);
textField.setText("");
textField.setChangedListener(this::onTextChanged);
this.addDrawableChild(textField);
// Set focus to the text field
......@@ -70,6 +69,7 @@ public class ChatScreen extends Screen {
.size(buttonWidth, buttonHeight)
.position(textFieldX + buttonWidth + buttonSpacing, buttonsY)
.build();
sendButton.active = false;
this.addDrawableChild(sendButton);
}
......@@ -83,10 +83,7 @@ public class ChatScreen extends Screen {
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_KP_ENTER) {
if (textField.isFocused()) {
// Play click sound
MinecraftClient.getInstance().player.playSound(SoundEvents.UI_BUTTON_CLICK.value(), 0.2F, 0.8F);
if (textField.isFocused() && !textField.getText().isEmpty()) {
// Close window on ENTER key press
sendChatMessage();
return true;
......@@ -95,6 +92,11 @@ public class ChatScreen extends Screen {
return super.keyPressed(keyCode, scanCode, modifiers); // Handle other key presses
}
private void onTextChanged(String text) {
// Enable the button only if the text field is not empty
sendButton.active = !text.isEmpty();
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
// Render the label text above the text field
......
......@@ -10,7 +10,6 @@ import net.minecraft.client.render.Camera;
import net.minecraft.entity.Entity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.slf4j.Logger;
......@@ -97,9 +96,6 @@ public class ClickHandler {
// Look-up conversation
ChatDataManager.EntityChatData chatData = ChatDataManager.getClientInstance().getOrCreateChatData(closestEntityUUID.toString());
// Play click sound
client.player.playSound(SoundEvents.UI_BUTTON_CLICK.value(), 0.2F, 0.8F);
// Determine area clicked inside chat bubble (top, left, right)
String hitRegion = determineHitRegion(closestHitResult.get(), closestBubbleData.position, camera, closestBubbleData.height);
LOGGER.info("Clicked region: " + hitRegion);
......
package com.owlmaddie.utils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import java.util.UUID;
/**
* The {@code ClientEntityFinder} class is used to find a specific MobEntity by UUID, since
* there is not a built-in method for this.
* there is not a built-in method for this. Also has a method for client PlayerEntity lookup.
*/
public class ClientEntityFinder {
public static MobEntity getEntityByUUID(ClientWorld world, UUID uuid) {
......@@ -19,4 +21,11 @@ public class ClientEntityFinder {
}
return null; // Entity not found
}
public static PlayerEntity getPlayerEntityFromUUID(UUID uuid) {
return MinecraftClient.getInstance().world.getPlayers().stream()
.filter(player -> player.getUuid().equals(uuid))
.findFirst()
.orElse(null);
}
}
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