Skip to content

Commit

Permalink
Improved global chat.
Browse files Browse the repository at this point in the history
  • Loading branch information
coltonk9043 committed Jan 18, 2024
1 parent 8c52863 commit f00a219
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
59 changes: 53 additions & 6 deletions src/main/java/net/aoba/cmd/GlobalChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.aoba.Aoba;
import net.aoba.settings.SettingManager;
import net.aoba.settings.types.BooleanSetting;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

public class GlobalChat {

Expand All @@ -21,8 +26,12 @@ public class GlobalChat {
private BufferedReader in;
private boolean started = false;

private BooleanSetting enabled;

public GlobalChat() {
gson = new Gson();
enabled = new BooleanSetting("global_chat_enabled", "Whether or not global chat is enabled or disabled.", true);
SettingManager.register_setting(this.enabled, Aoba.getInstance().settingManager.modules_category);
}

private void Send(String json) {
Expand All @@ -34,6 +43,14 @@ public void SendMessage(String message) {
Send(gson.toJson(new MessageAction(message, null)));
}

private void SendChatMessage(String message) {
MinecraftClient mc = MinecraftClient.getInstance();
if(mc.inGameHud != null) {
mc.inGameHud.getChatHud().addMessage(Text.of(Formatting.DARK_PURPLE + "[" + Formatting.LIGHT_PURPLE + "GLOBAL" + Formatting.DARK_PURPLE + "] " + Formatting.RESET + message));
}
}


public void StartListener() {
if(started) {
System.out.println("Socket listener already started.");
Expand All @@ -42,7 +59,9 @@ public void StartListener() {

try {
started = true;
socket = new Socket("18.119.121.174", 80);

// Gotta love AWS!
socket = new Socket("18.221.222.43", 80);
out = new PrintWriter(socket.getOutputStream(), false);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

Expand All @@ -54,11 +73,14 @@ public void run() {
Send(gson.toJson(new ConnectAction(MinecraftClient.getInstance().getSession().getUsername())));

String json = in.readLine();
while(json != null) {
Map<String, String> map = new Gson().fromJson(json, new TypeToken<HashMap<String, String>>() {}.getType());
if(map.containsKey("message")) {
String message = map.get("message");
CommandManager.sendChatMessage(message);
while(json != null && MinecraftClient.getInstance() != null) {
MessageResponse response = new Gson().fromJson(json, MessageResponse.class);
if(response != null) {
String user = response.getUser();
String chatMessage = response.getMessage();
if(user != null && chatMessage != null) {
SendChatMessage(String.format("<%s> %s", user, chatMessage));
}
}
json = in.readLine();
}
Expand Down Expand Up @@ -108,4 +130,29 @@ public MessageAction(String message, String to) {
}
}

/**
* Server Response
*/
class MessageResponse {
private String message;
private String user;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}
}



3 changes: 2 additions & 1 deletion src/main/java/net/aoba/mixin/ChatScreenMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public void onSendMessage(String message, boolean addToHistory, CallbackInfoRetu
Aoba.getInstance().commandManager.command(message.split(" "));
cir.setReturnValue(true);
}else if (message.startsWith(".global")) {
Aoba.getInstance().globalChat.SendMessage(message);
Aoba.getInstance().globalChat.SendMessage(message.substring(8, message.length()));
cir.setReturnValue(true);
}
}
}
10 changes: 6 additions & 4 deletions src/main/java/net/aoba/module/ModuleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,12 @@ public Module getModuleByName(String string) {

@Override
public void OnKeyDown(KeyDownEvent event) {
for(Module module : modules) {
Key binding = module.getBind().getValue();
if(binding.getCode() == event.GetKey()) {
module.toggle();
if(MinecraftClient.getInstance().currentScreen == null) {
for(Module module : modules) {
Key binding = module.getBind().getValue();
if(binding.getCode() == event.GetKey()) {
module.toggle();
}
}
}
}
Expand Down

0 comments on commit f00a219

Please sign in to comment.