Skip to content

Commit

Permalink
Add MuteLobbyChat and improve ToggleSpecificAds
Browse files Browse the repository at this point in the history
  • Loading branch information
udu3324 committed Jun 20, 2024
1 parent 9ec645f commit 2510cc5
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/udu3324/poinpow/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ public static void create() {
BlockRaids.toggled.set(Boolean.parseBoolean(getValueFromConfig(HubCommandBack.name)));
BlockChestAds.toggled.set(Boolean.parseBoolean(getValueFromConfig(BlockChestAds.name)));

MuteLobbyChat.toggled = Boolean.parseBoolean(getValueFromConfig(MuteLobbyChat.name));

defaultRank = Boolean.parseBoolean(getValueFromConfig(name + "_default"));
vip = Boolean.parseBoolean(getValueFromConfig(name + "_vip"));
vipPlus = Boolean.parseBoolean(getValueFromConfig(name + "_vipPlus"));
Expand Down Expand Up @@ -268,6 +270,7 @@ private static void writeDefaultConfig() throws IOException {
w.write(HubCommandBack.name + ": true" + System.lineSeparator());
w.write(BlockRaids.name + ": true" + System.lineSeparator());
w.write(BlockChestAds.name + ": true" + System.lineSeparator());
w.write(MuteLobbyChat.name + ": false" + System.lineSeparator());

w.write(name + "_default: false" + System.lineSeparator());
w.write(name + "_vip: false" + System.lineSeparator());
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/com/udu3324/poinpow/api/Minehut.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,52 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.udu3324.poinpow.Poinpow;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.scoreboard.Scoreboard;
import net.minecraft.scoreboard.ScoreboardObjective;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Minehut {
public static Map<String, String> playerRank = new HashMap<>();

public static Boolean inLobby() {
if (MinecraftClient.getInstance().player == null) return null;

// check for scoreboard
Scoreboard scoreboard = MinecraftClient.getInstance().player.getScoreboard();
ArrayList<String> scores = new ArrayList<>();
for (ScoreboardObjective objective : scoreboard.getObjectives()) {
scores.add(objective.getDisplayName().toString());
}

return scores.toString().toLowerCase().contains("minehut");
}

//(BuggyAl) this gets information about a server in json
public static JsonObject getServer(ClientPlayerEntity player, String serverName) {
try {
URL apiURL = new URL("https://api.minehut.com/server/" + serverName + "?byName=true");
HttpURLConnection connection = (HttpURLConnection) apiURL.openConnection();
connection.setRequestMethod("GET");

if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
if (connection.getResponseCode() == 500) {
player.sendMessage(Text.literal("Server not found! (" + connection.getResponseCode() + ")").styled(style -> style
.withColor(Formatting.RED)));
return null;
} else if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { // bruh
player.sendMessage(Text.literal("Server not found! API may be down. (" + connection.getResponseCode() + ")").styled(style -> style
.withColor(Formatting.RED)));
return null;
}

JsonObject obj = JsonParser.parseReader(new InputStreamReader(connection.getInputStream())).getAsJsonObject().get("server").getAsJsonObject();
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/udu3324/poinpow/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class Commands {
//register poinpow commands in the mc brigadier
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
ServerLookup.registerCommand(dispatcher);
MuteLobbyChat.registerCommand(dispatcher);

dispatcher.register(literal("poinpow")
.executes(ctx -> help(ctx.getSource()))
Expand Down Expand Up @@ -193,6 +194,13 @@ private static int help(FabricClientCommandSource source) {
.withColor(Formatting.DARK_GRAY)
));

//toggle lobby chat
source.sendFeedback(Text.literal("[" + MuteLobbyChat.toggled + "] " + MuteLobbyChat.name).styled(style -> style
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal(MuteLobbyChat.description + "\n\nClick to Toggle")))
.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + MuteLobbyChat.name))
.withColor(Formatting.DARK_GRAY)
));

//chat phrase filter
source.sendFeedback(Text.literal("/poinpow " + ChatPhraseFilter.name + " <true/false/list/add/remove>").styled(style -> style
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal(ChatPhraseFilter.description + "\n\nClick to change")))
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/com/udu3324/poinpow/mixin/ChatMixin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.udu3324.poinpow.mixin;

import com.udu3324.poinpow.api.Minehut;
import com.udu3324.poinpow.commands.Commands;
import com.udu3324.poinpow.utils.*;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.client.gui.hud.MessageIndicator;
import net.minecraft.network.message.MessageSignatureData;
Expand All @@ -13,13 +15,23 @@

@Mixin(ChatHud.class)
public class ChatMixin {
@Inject(method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;Lnet/minecraft/client/gui/hud/MessageIndicator;)V", at = @At("HEAD"), cancellable = true)
@Inject(method = "Lnet/minecraft/client/gui/hud/ChatHud;addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;Lnet/minecraft/client/gui/hud/MessageIndicator;)V", at = @At("HEAD"), cancellable = true)
private void onMessage(Text message, MessageSignatureData signature, MessageIndicator indicator, CallbackInfo ci) {
String chat = message.getString();

//don't allow blockers to run while help command is running
if (Commands.running) return;

//if lobby chat is toggled, don't run checks
if (MuteLobbyChat.toggled) {
// if they're in lobby & the message isn't allowed, cancel.
Boolean inLobby = Minehut.inLobby();
if (inLobby != null && inLobby && !MuteLobbyChat.isAllowed(chat)) {
ci.cancel();
return;
}
}

if (BlockLobbyWelcome.check(chat, ci)) return;

if (BlockLobbyAds.check(chat, ci)) return;
Expand All @@ -32,4 +44,9 @@ private void onMessage(Text message, MessageSignatureData signature, MessageIndi

ChatPhraseFilter.check(chat, ci);
}

@Inject(method = "Lnet/minecraft/client/gui/hud/ChatHud;render(Lnet/minecraft/client/gui/DrawContext;IIIZ)V", at = @At("RETURN"), cancellable = true)
private void onRender(DrawContext context, int currentTick, int mouseX, int mouseY, boolean focused, CallbackInfo ci) {
MuteLobbyChat.check();
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/udu3324/poinpow/mixin/WorldLoadedMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.udu3324.poinpow.Poinpow;
import com.udu3324.poinpow.api.GitHubVersion;
import com.udu3324.poinpow.utils.AutoSkipBarrier;
import com.udu3324.poinpow.utils.MuteLobbyChat;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.client.MinecraftClient;
Expand All @@ -28,5 +29,8 @@ private static void onLoad(ClientPlayConnectionEvents.Join[] callbacks, ClientPl
if (Poinpow.onMinehut) GitHubVersion.check();

AutoSkipBarrier.check();

//allow to send a check only once cause chat gets rendered per tick
MuteLobbyChat.canSendCheck = true;
}
}
17 changes: 3 additions & 14 deletions src/main/java/com/udu3324/poinpow/utils/BlockLobbyMapAds.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.udu3324.poinpow.utils;

import com.udu3324.poinpow.Poinpow;
import net.minecraft.client.MinecraftClient;
import com.udu3324.poinpow.api.Minehut;
import net.minecraft.client.gui.hud.ClientBossBar;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.entity.Entity;
import net.minecraft.entity.decoration.ItemFrameEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.scoreboard.Scoreboard;
import net.minecraft.scoreboard.ScoreboardObjective;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.ArrayList;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -31,16 +28,8 @@ public static void block(Entity entity) {
// return if not on minehut
if (!Poinpow.onMinehut) return;

if (MinecraftClient.getInstance().player == null) return;

// check for scoreboard
Scoreboard scoreboard = MinecraftClient.getInstance().player.getScoreboard();
ArrayList<String> scores = new ArrayList<>();
for (ScoreboardObjective objective : scoreboard.getObjectives()) {
scores.add(objective.getDisplayName().toString());
}

if (!scores.toString().toLowerCase().contains("minehut")) return;
Boolean inLobby = Minehut.inLobby();
if (inLobby == null || !inLobby) return;

// remove if item frame has a map in it
ItemFrameEntity itemFrame = (ItemFrameEntity) entity;
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/com/udu3324/poinpow/utils/MuteLobbyChat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.udu3324.poinpow.utils;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.udu3324.poinpow.Config;
import com.udu3324.poinpow.api.Minehut;
import com.udu3324.poinpow.commands.Commands;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

public class MuteLobbyChat {
public static String name = "muteLobbyChat";
public static String description = "This mutes the lobby chat if you don't want to see it.";

public static boolean toggled = false;
public static boolean canSendCheck = false;

public static void registerCommand(CommandDispatcher<FabricClientCommandSource> dispatcher) {
dispatcher.register(ClientCommandManager.literal(name).executes(ctx -> toggle(ctx.getSource())));
}

private static int toggle(FabricClientCommandSource source) {
Commands.running = true;

//don't allow check as they already know
canSendCheck = false;

if (toggled) {
source.sendFeedback(Text.literal("Lobby chat has been unmuted.").styled(style -> style
.withColor(Formatting.RED)));

toggled = false;
} else {
source.sendFeedback(Text.literal("Lobby chat has been muted.").styled(style -> style
.withColor(Formatting.GREEN)));

toggled = true;
}

Config.setValueFromConfig(name, String.valueOf(toggled));

Commands.running = false;
return Command.SINGLE_SUCCESS;
}

public static boolean isAllowed(String chat) {
if (MinecraftClient.getInstance().player == null || MinecraftClient.getInstance().player.getName().getLiteralString() == null) return false;

//it is a /msg from someone
if (chat.startsWith("To") || chat.startsWith("From")) {
//it is a /msg from someone
return true;
} else if (!chat.contains(":")) {
//misc message should be allowed
return true;
} else if (chat.toLowerCase().contains(MinecraftClient.getInstance().player.getName().getLiteralString().toLowerCase())) {
//messages with the player's ign
return true;
} else {
return false;
}
}

//this is run when joining back into the lobby while it is muted.
public static void check() {
if (!canSendCheck) return;

if (!toggled) return;

Boolean inLobby = Minehut.inLobby();
if (inLobby != null && inLobby) {
MinecraftClient.getInstance().inGameHud.getChatHud().addMessage(Text.literal("Lobby is currently muted by you. (click)").styled(style -> style
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/" + MuteLobbyChat.name))
.withColor(Formatting.DARK_GRAY)));
}

canSendCheck = false;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/udu3324/poinpow/utils/ServerLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mojang.brigadier.arguments.StringArgumentType;
import com.udu3324.poinpow.Poinpow;
import com.udu3324.poinpow.api.Minehut;
import com.udu3324.poinpow.commands.Commands;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.MinecraftClient;
Expand Down Expand Up @@ -56,6 +57,8 @@ private static int lookup(String serverName) {
status = Formatting.GREEN + "Online";
}

Commands.running = true;

player.sendMessage(Text.literal(""));
player.sendMessage(Text.literal(serverName + " is ").styled(style -> style
.withColor(Formatting.GOLD))
Expand Down Expand Up @@ -104,6 +107,8 @@ private static int lookup(String serverName) {
}

player.sendMessage(Text.literal(""));

Commands.running = false;
}).start();

return Command.SINGLE_SUCCESS;
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/com/udu3324/poinpow/utils/ToggleSpecificAds.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public static boolean checkRank(String chat) {
//use mh api to get their rank instead of parsing untrusted chat
String rank = Minehut.getRank(uuid);

//wi-fi errors, etc. no clue why this scenario would ever happen
//wi-fi errors, etc. no clue why this scenario would ever happen... :)
if (rank == null) {
return false;
return fallback(chat);
}

System.out.println("rank: " + rank);
//System.out.println("rank: " + rank);

//check if their rank is toggled or not
if (rank.equals("VIP") && vip) {
Expand All @@ -65,6 +65,24 @@ public static boolean checkRank(String chat) {
} else return rank.equals("DEFAULT") && defaultRank;
}

// fallback in case if mh api does not work (minehut sucks, and their staff are probably underpaid to do stuff)
private static boolean fallback(String chat) {
String rank = chat.substring(0, chat.indexOf(":"));

//check if the rank portion in chat contains it
if (rank.contains("[VIP]") && vip) {
return true;
} else if (rank.contains("[VIP+]") && vipPlus) {
return true;
} else if (rank.contains("[PRO]") && pro) {
return true;
} else if (rank.contains("[LEGEND]") && legend) {
return true;
} else if (rank.contains("[PATRON]") && patron) {
return true;
} else return !(rank.contains("[PATRON]") || rank.contains("[LEGEND]") || rank.contains("[PRO]") || rank.contains("[VIP+]") || rank.contains("[VIP]")) && defaultRank;
}

public static int toggle(FabricClientCommandSource source, String rank) {
Commands.running = true;

Expand Down
Binary file added src/main/resources/assets/poinpow/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2510cc5

Please sign in to comment.