Skip to content

Commit

Permalink
Remove unlicensed code and optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqlvin committed Sep 3, 2023
1 parent 3647f55 commit 8dddcc0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 58 deletions.
23 changes: 15 additions & 8 deletions src/main/java/me/mqlvin/wwp/feature/SideCount.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package me.mqlvin.wwp.feature;

import com.google.common.eventbus.Subscribe;
import me.mqlvin.wwp.WoolWarsPlus;
import me.mqlvin.wwp.util.ScoreboardUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.scoreboard.ScorePlayerTeam;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
Expand Down Expand Up @@ -37,23 +37,30 @@ public void render(RenderGameOverlayEvent.Text event) {

public void updateCount() {
if(!roundActive) return;
List<String> sbLines = ScoreboardUtils.getSidebarScores(Minecraft.getMinecraft().theWorld.getScoreboard());
List<String> sbLines = ScoreboardUtils.getSidebarSuffixes(Minecraft.getMinecraft().theWorld.getScoreboard());

if(sbLines.size() != 14) { blueCount = 4; redCount = 4; return; }; // not the right scoreboard or time
if(sbLines.size() != 8) { return; }; // not the right scoreboard or time

String possibleRedCount = sbLines.stream().filter(s -> s.contains("§c§r")).findFirst().orElse(null);
String possibleBlueCount = sbLines.stream().filter(s -> s.contains("§9§r")).findFirst().orElse(null);

if(possibleRedCount == null || possibleBlueCount == null) { return; } // if lines aren't there return
if(possibleRedCount == null || possibleBlueCount == null) { redCount = 4; blueCount = 4; return; } // if lines aren't there return

try {
possibleRedCount = possibleRedCount.substring(5); // football emoji - one char - removes emoji/colour code
possibleBlueCount = possibleBlueCount.substring(6); // basketball emoji - 2 chars - removes emoji/colour code
redCount = Integer.parseInt(String.valueOf(possibleRedCount.charAt(0)));
possibleRedCount = possibleRedCount.substring(4); // removes emoji/colour code
possibleBlueCount = possibleBlueCount.substring(4);
redCount = Integer.parseInt(String.valueOf(possibleRedCount.charAt(0))); // there is a trailing space at the end
blueCount = Integer.parseInt(String.valueOf(possibleBlueCount.charAt(0)));
} catch (Exception ignore) {}
}

@SubscribeEvent
public void onChat(ClientChatReceivedEvent event) {
if(event.message.getUnformattedText().startsWith(" ") && event.message.getUnformattedText().contains("Round")) {
blueCount = 4; redCount = 4;
}
}



public void updateRenderPosition() {
Expand Down Expand Up @@ -105,7 +112,7 @@ public void render(int r, int b) {
}

public boolean isInGame() {
List<String> sbLines = Minecraft.getMinecraft().theWorld.getScoreboard().getTeams().stream().map(ScorePlayerTeam::getColorPrefix).collect(Collectors.toList());
List<String> sbLines = ScoreboardUtils.getSidebarPrefixes(Minecraft.getMinecraft().theWorld.getScoreboard());
List<String> info = sbLines.stream().filter(s -> s.contains("⬤") || s.equals("State: §ePre Rou") || s.equals("State: §eActive ")).collect(Collectors.toList());

return info.size() == 3; // valid state, red/blue team rounds won indicator
Expand Down
54 changes: 4 additions & 50 deletions src/main/java/me/mqlvin/wwp/util/ScoreboardUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package me.mqlvin.wwp.util;

import net.minecraft.client.Minecraft;
import net.minecraft.scoreboard.Score;
import net.minecraft.scoreboard.ScoreObjective;
import net.minecraft.scoreboard.ScorePlayerTeam;
import net.minecraft.scoreboard.Scoreboard;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;

import java.util.ArrayList;
import java.util.Comparator;
Expand All @@ -20,55 +17,12 @@
*
*/
public class ScoreboardUtils {
public static List<String> getSidebarSuffixes(Scoreboard scoreboard) {

/**
* Filters scores from a scoreboards objective and returns the values in a list
*
* Output was designed around Hypixels scoreboard setup.
*
* @param scoreboard the target scoreboard
* @return returns an empty list if no scores were found.
*/
public static List<String> getSidebarScores(Scoreboard scoreboard) {
List<String> found = new ArrayList<>();

ScoreObjective sidebar = scoreboard.getObjectiveInDisplaySlot(1);
if (sidebar != null) {
List<Score> scores = new ArrayList<>(scoreboard.getScores());
/*Scores retrieved here do not care for ordering, this is done by the Scoreboard its self.
We'll need to do this our selves in this case.
This will appear backwars in chat, but remember that the scoreboard reverses this order
to ensure highest scores go first.
*/
scores.sort(Comparator.comparingInt(Score::getScorePoints));

found = scores.stream()
.filter(score -> score.getObjective().getName().equals(sidebar.getName()))
.map(score -> score.getPlayerName() + getSuffixFromContainingTeam(scoreboard, score.getPlayerName()))
.collect(Collectors.toList());
}

return found;
return scoreboard.getTeams().stream().map(ScorePlayerTeam::getColorSuffix).filter(s -> !s.isEmpty()).collect(Collectors.toList());
}

/**
* Filters through Scoreboard teams searching for a team
* that contains the last part of our scoreboard message.
*
*
* @param scoreboard The target scoreboard
* @param member The message we're searching for inside a teams member collection.
* @return If no team was found, an empty suffix is returned
*/
private static String getSuffixFromContainingTeam(Scoreboard scoreboard, String member) {
String suffix = null;
for (ScorePlayerTeam team : scoreboard.getTeams()) {
if (team.getMembershipCollection().contains(member)) {
suffix = team.getColorSuffix();
break;
}
}
return (suffix == null ? "" : suffix);
public static List<String> getSidebarPrefixes(Scoreboard scoreboard) {
return scoreboard.getTeams().stream().map(ScorePlayerTeam::getColorPrefix).collect(Collectors.toList());
}
}

0 comments on commit 8dddcc0

Please sign in to comment.