fix kaboom check

This commit is contained in:
Chayapak 2023-09-25 19:11:57 +07:00
parent bc738a6f25
commit d931a8abd1

View file

@ -1,28 +1,20 @@
package land.chipmunk.chipmunkmod.modules;
import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.suggestion.Suggestions;
import land.chipmunk.chipmunkmod.listeners.Listener;
import land.chipmunk.chipmunkmod.listeners.ListenerManager;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.SubtitleS2CPacket;
import net.minecraft.network.packet.s2c.play.TitleS2CPacket;
import net.minecraft.network.packet.s2c.play.CommandSuggestionsS2CPacket;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CompletableFuture;
public class KaboomCheck extends Listener {
public static final String TITLE_START_TEXT = "Welcome to ";
public static final String TITLE_END_TEXT = "!";
public static final String SUBTITLE_START_TEXT = "Free OP";
public boolean isKaboom = false;
private boolean hasKaboomTitle = false;
private boolean hasKaboomSubtitle = false;
private Timer timer = null;
private final MinecraftClient client;
@ -49,50 +41,43 @@ public class KaboomCheck extends Listener {
timer = new Timer();
timer.schedule(task, 50, 50);
check();
}
private void tick () {
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
if (networkHandler == null) cleanup();
}
if (hasKaboomTitle && hasKaboomSubtitle) isKaboom = true;
private void check () {
final CompletableFuture<CommandSuggestionsS2CPacket> future = TabComplete.INSTANCE.complete("/ver ");
future.thenApply((packet) -> {
final Suggestions suggestions = packet.getSuggestions();
for (int i = 0; i < suggestions.getList().size(); i++) {
final Suggestion suggestion = suggestions.getList().get(i);
if (suggestion.getText().equals("Extras")) {
isKaboom = true;
break;
}
}
return packet;
});
}
private void cleanup () {
if (timer == null) return;
isKaboom = false;
hasKaboomTitle = false;
hasKaboomSubtitle = false;
timer.purge();
timer.cancel();
}
@Override
public void packetReceived(Packet<?> packet) {
if (packet instanceof TitleS2CPacket) packetReceived((TitleS2CPacket) packet);
else if (packet instanceof SubtitleS2CPacket) packetReceived((SubtitleS2CPacket) packet);
}
// TODO: move this to a util class
private String stripSectionSigns (String text) {
return text.replaceAll("§.", "");
}
public void packetReceived(TitleS2CPacket packet) {
final String stripped = stripSectionSigns(packet.getTitle().getString());
if (
stripped.startsWith(TITLE_START_TEXT) &&
stripped.endsWith(TITLE_END_TEXT)
) hasKaboomTitle = true;
}
public void packetReceived(SubtitleS2CPacket packet) {
final String stripped = stripSectionSigns(packet.getSubtitle().getString());
if (stripped.startsWith(SUBTITLE_START_TEXT)) hasKaboomSubtitle = true;
}
}