diff options
Diffstat (limited to 'src/main/java/com/thatgravyboat/skyblockhud/api')
5 files changed, 134 insertions, 0 deletions
diff --git a/src/main/java/com/thatgravyboat/skyblockhud/api/LeaderboardGetter.java b/src/main/java/com/thatgravyboat/skyblockhud/api/LeaderboardGetter.java new file mode 100644 index 0000000..58cede8 --- /dev/null +++ b/src/main/java/com/thatgravyboat/skyblockhud/api/LeaderboardGetter.java @@ -0,0 +1,65 @@ +package com.thatgravyboat.skyblockhud.api; + +import com.thatgravyboat.skyblockhud.Utils; +import com.thatgravyboat.skyblockhud.api.events.SidebarLineUpdateEvent; +import com.thatgravyboat.skyblockhud.api.events.SidebarPostEvent; +import com.thatgravyboat.skyblockhud.api.events.SidebarPreGetEvent; +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.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent; + +import java.util.*; +import java.util.stream.Collectors; + +import static com.thatgravyboat.skyblockhud.ComponentHandler.SCOREBOARD_CHARACTERS; + +public class LeaderboardGetter { + + private static Map<Integer, String> cachedScores = new HashMap<>(); + private static List<String> cachedScoresList = new ArrayList<>(); + + private static int ticks = 0; + + //This is really bad and should use the packet instead. + + @SubscribeEvent + public void onClientUpdate(TickEvent.ClientTickEvent event){ + if (event.phase.equals(TickEvent.Phase.START)) return; + ticks++; + if (ticks % 5 != 0) return; + + Minecraft mc = Minecraft.getMinecraft(); + if (mc.theWorld != null) { + Scoreboard scoreboard = mc.theWorld.getScoreboard(); + ScoreObjective sidebarObjective = scoreboard.getObjectiveInDisplaySlot(1); + + if (sidebarObjective != null && !MinecraftForge.EVENT_BUS.post(new SidebarPreGetEvent(scoreboard, sidebarObjective))) { + Collection<Score> scoreList = sidebarObjective.getScoreboard().getSortedScores(sidebarObjective); + Map<Integer, String> scores = scoreList.stream().collect(Collectors.toMap(Score::getScorePoints, this::getLine)); + + if (!cachedScores.equals(scores)) { + scores.forEach((score, name) -> { + if (cachedScores.get(score) == null || !cachedScores.get(score).equals(name)) { + MinecraftForge.EVENT_BUS.post(new SidebarLineUpdateEvent(name, SCOREBOARD_CHARACTERS.matcher(name).replaceAll("").trim(), score, scores.size(), scoreboard, sidebarObjective)); + } + }); + cachedScores = scores; + cachedScoresList = scores.values().stream().map(name -> SCOREBOARD_CHARACTERS.matcher(name).replaceAll("").trim()).collect(Collectors.toList()); + } + MinecraftForge.EVENT_BUS.post(new SidebarPostEvent(scoreboard, sidebarObjective, cachedScoresList)); + } + } + } + + public String getLine(Score score) { + ScorePlayerTeam scorePlayerTeam = score.getScoreScoreboard().getPlayersTeam(score.getPlayerName()); + return Utils.removeColor(ScorePlayerTeam.formatPlayerName(scorePlayerTeam, score.getPlayerName())); + } + + +} diff --git a/src/main/java/com/thatgravyboat/skyblockhud/api/events/ProfileSwitchedEvent.java b/src/main/java/com/thatgravyboat/skyblockhud/api/events/ProfileSwitchedEvent.java new file mode 100644 index 0000000..015388a --- /dev/null +++ b/src/main/java/com/thatgravyboat/skyblockhud/api/events/ProfileSwitchedEvent.java @@ -0,0 +1,7 @@ +package com.thatgravyboat.skyblockhud.api.events; + +import net.minecraftforge.fml.common.eventhandler.Event; + +public class ProfileSwitchedEvent extends Event { + +} diff --git a/src/main/java/com/thatgravyboat/skyblockhud/api/events/SidebarLineUpdateEvent.java b/src/main/java/com/thatgravyboat/skyblockhud/api/events/SidebarLineUpdateEvent.java new file mode 100644 index 0000000..2737ee9 --- /dev/null +++ b/src/main/java/com/thatgravyboat/skyblockhud/api/events/SidebarLineUpdateEvent.java @@ -0,0 +1,22 @@ +package com.thatgravyboat.skyblockhud.api.events; + +import net.minecraft.scoreboard.ScoreObjective; +import net.minecraft.scoreboard.Scoreboard; +import net.minecraftforge.fml.common.eventhandler.Event; + +public class SidebarLineUpdateEvent extends Event { + + public String rawLine; + public String formattedLine; + public int position; + public Scoreboard scoreboard; + public ScoreObjective objective; + + public SidebarLineUpdateEvent(String rawLine, String formattedLine, int score, int max, Scoreboard scoreboard, ScoreObjective objective) { + this.rawLine = rawLine; + this.formattedLine = formattedLine; + this.position = max - score; + this.scoreboard = scoreboard; + this.objective = objective; + } +} diff --git a/src/main/java/com/thatgravyboat/skyblockhud/api/events/SidebarPostEvent.java b/src/main/java/com/thatgravyboat/skyblockhud/api/events/SidebarPostEvent.java new file mode 100644 index 0000000..b81859a --- /dev/null +++ b/src/main/java/com/thatgravyboat/skyblockhud/api/events/SidebarPostEvent.java @@ -0,0 +1,22 @@ +package com.thatgravyboat.skyblockhud.api.events; + +import net.minecraft.scoreboard.ScoreObjective; +import net.minecraft.scoreboard.Scoreboard; +import net.minecraftforge.fml.common.eventhandler.Event; + +import java.util.List; + +public class SidebarPostEvent extends Event { + + public Scoreboard scoreboard; + public ScoreObjective objective; + public List<String> scores; + public String[] arrayScores; + + public SidebarPostEvent(Scoreboard scoreboard, ScoreObjective objective, List<String> scores) { + this.scoreboard = scoreboard; + this.objective = objective; + this.scores = scores; + this.arrayScores = scores.toArray(new String[]{}); + } +} diff --git a/src/main/java/com/thatgravyboat/skyblockhud/api/events/SidebarPreGetEvent.java b/src/main/java/com/thatgravyboat/skyblockhud/api/events/SidebarPreGetEvent.java new file mode 100644 index 0000000..0db1895 --- /dev/null +++ b/src/main/java/com/thatgravyboat/skyblockhud/api/events/SidebarPreGetEvent.java @@ -0,0 +1,18 @@ +package com.thatgravyboat.skyblockhud.api.events; + +import net.minecraft.scoreboard.ScoreObjective; +import net.minecraft.scoreboard.Scoreboard; +import net.minecraftforge.fml.common.eventhandler.Cancelable; +import net.minecraftforge.fml.common.eventhandler.Event; + +@Cancelable +public class SidebarPreGetEvent extends Event { + + public Scoreboard scoreboard; + public ScoreObjective objective; + + public SidebarPreGetEvent(Scoreboard scoreboard, ScoreObjective objective) { + this.scoreboard = scoreboard; + this.objective = objective; + } +} |