Advertisement
Guest User

Untitled

a guest
Jun 4th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. package net.mcreator.myfriendmava;
  2.  
  3. import net.minecraftforge.event.ServerChatEvent;
  4. import net.minecraftforge.eventbus.api.SubscribeEvent;
  5. import net.minecraftforge.fml.common.Mod;
  6. import net.minecraftforge.api.distmarker.Dist;
  7. import com.google.gson.JsonObject;
  8. import com.google.gson.JsonParser;
  9. import net.minecraft.network.chat.Component;
  10. import net.mcreator.myfriendmava.OpenAIClient;
  11.  
  12. import java.io.IOException;
  13.  
  14. @Mod.EventBusSubscriber(modid = "myfriendmava", value = Dist.CLIENT)
  15. public class ChatHandler {
  16.  
  17. private static final OpenAIClient openAIClient = new OpenAIClient();
  18.  
  19. @SubscribeEvent
  20. public static void onChatMessage(ServerChatEvent event) {
  21. String message = event.getMessage().getString();
  22. if (message.contains("Mava")) {
  23. new Thread(() -> {
  24. try {
  25. String characterPrompt = "You are Mava, a friendly and loyal companion who loves to protect and help your owner. "
  26. + "Respond to this message in a kind and enthusiastic manner: " + message;
  27.  
  28. String response = openAIClient.getResponse(characterPrompt);
  29.  
  30. //JSON-respond JsonObject jsonResponse = JsonParser.parseString(response).getAsJsonObject();
  31. String reply = jsonResponse.getAsJsonArray("choices").get(0).getAsJsonObject().get("text").getAsString().trim();
  32.  
  33. event.getPlayer().sendSystemMessage(Component.literal("Mava: " + reply));
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }).start();
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement