Skip to content

Commit a58031e

Browse files
committed
Added pinning scores to the top of the list!
1 parent 2b4de05 commit a58031e

4 files changed

Lines changed: 54 additions & 10 deletions

File tree

src/main/java/space/cutekitten/commandhelper/client/ClientDB.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class ClientDB {
1010
public static MinecraftClient client = MinecraftClient.getInstance();
1111
public static List<ScoreboardPlayerScore> scores = new ArrayList<>();
1212
public static List<ScoreboardPlayerScore> showScores = new ArrayList<>();
13+
public static List<ScoreboardPlayerScore> pinnedScores = new ArrayList<>();
1314
public static String currentSearch = "";
1415
public static boolean customScoreboardActive = true;
1516
}

src/main/java/space/cutekitten/commandhelper/client/ScoreboardRenderer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,21 @@
1010
import space.cutekitten.commandhelper.mixin.HandledScreenAccessor;
1111

1212
public class ScoreboardRenderer {
13-
public static void renderScore(CreativeInventoryScreen screen, MatrixStack matrices, ScoreboardPlayerScore score, int index) {
13+
public static void renderScore(CreativeInventoryScreen screen, MatrixStack matrices,
14+
ScoreboardPlayerScore score, int index, boolean pinned) {
1415
TextRenderer textRenderer = ClientDB.client.textRenderer;
1516

1617
int x = ((HandledScreenAccessor) screen).getX() + 9;
1718
int y = ((HandledScreenAccessor) screen).getY() + 18 + index * 18;
1819

20+
int colour = 0xFFFFFFFF;
21+
if (pinned) {
22+
colour = 0xFFFFFF00; // yellow
23+
}
1924
// main background
2025
fill(matrices.peek().getPositionMatrix(),
2126
x - 1, y - 1, x + 18 * 9 - 2 + 1, y + 18 - 2 + 1, // rect bounds
22-
0xFFFFFFFF, // argb color
27+
colour, // argb color
2328
screen.getZOffset());
2429
fill(matrices.peek().getPositionMatrix(),
2530
x, y, x + 18 * 9 - 2, y + 18 - 2, // rect bounds

src/main/java/space/cutekitten/commandhelper/mixin/CreativeInventoryScreenMixin.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private void render(MatrixStack matrices, int mouseX, int mouseY, float delta, C
5555

5656
for (int i = 0; i < Math.min(ClientDB.showScores.size(), 5); i++) {
5757
ScoreboardPlayerScore score = ClientDB.showScores.get(i);
58-
ScoreboardRenderer.renderScore(screen, matrices, score, i);
58+
ScoreboardRenderer.renderScore(screen, matrices, score, i, ClientDB.pinnedScores.contains(score));
5959
}
6060

6161
for (int i = 0; i < Math.min(ClientDB.showScores.size(), 5); i++) {
@@ -156,14 +156,28 @@ private void search(CallbackInfo ci) {
156156
}
157157
}
158158

159-
// don't sort if the amount of scores is too high!!!
159+
Collator collator = Collator.getInstance(Locale.ROOT);
160+
ClientDB.pinnedScores.sort((score1, score2) -> {
161+
String score1Name = score1.getPlayerName();
162+
String score2Name = score2.getPlayerName();
163+
return collator.compare(score1Name, score2Name);
164+
});
165+
List<ScoreboardPlayerScore> sortedPins =
166+
ClientDB.pinnedScores.stream().filter(score ->
167+
score.getPlayerName().toLowerCase(Locale.ROOT).contains(searched)).toList();
168+
169+
// if the amount of scores is too high
160170
if (ClientDB.scores.size() > 10000) {
171+
ClientDB.scores.removeAll(sortedPins);
172+
ClientDB.scores.addAll(0, sortedPins);
161173
return;
162174
}
163175

164176
// hopefully faster sorting than just sorting the whole list at once
165177
HashMap<Character, List<ScoreboardPlayerScore>> scoreByFirstChar = new HashMap<>();
166178
for (ScoreboardPlayerScore score : ClientDB.scores) {
179+
if (sortedPins.contains(score)) continue;
180+
167181
char firstChar = score.getPlayerName().charAt(0);
168182
if (!scoreByFirstChar.containsKey(firstChar)) {
169183
scoreByFirstChar.put(firstChar, new ArrayList<>());
@@ -172,8 +186,8 @@ private void search(CallbackInfo ci) {
172186
}
173187

174188
ClientDB.scores.clear();
189+
ClientDB.scores.addAll(sortedPins);
175190

176-
Collator collator = Collator.getInstance(Locale.ROOT);
177191
for (char key : scoreByFirstChar.keySet().stream().sorted().toList()) {
178192
List<ScoreboardPlayerScore> sortedScores = scoreByFirstChar.get(key);
179193
sortedScores.sort((score1, score2) -> {
@@ -198,4 +212,28 @@ private void mouseScrolled(double mouseX, double mouseY, double amount, Callback
198212
((CreativeInventoryScreen.CreativeScreenHandler)ClientDB.client.player.currentScreenHandler).scrollItems(this.scrollPosition);
199213
cir.setReturnValue(true);
200214
}
215+
216+
@Inject(method = "mouseClicked", at = @At("HEAD"))
217+
private void onMouseClick(double mouseX, double mouseY, int button, CallbackInfoReturnable<Boolean> cir) {
218+
if (selectedTab != CommandHelper.ITEM_GROUP.getIndex()) return;
219+
if (ClientDB.client.player == null) return;
220+
if (button != 0) return;
221+
222+
for (int i = 0; i < Math.min(ClientDB.showScores.size(), 5); i++) {
223+
ScoreboardPlayerScore score = ClientDB.showScores.get(i);
224+
225+
if (((HandledScreenAccessor)this).invokeIsPointWithinBounds(
226+
9,
227+
18 + 18 * i,
228+
18*9, 16, mouseX, mouseY)) {
229+
if (ClientDB.pinnedScores.contains(score)) {
230+
ClientDB.pinnedScores.remove(score);
231+
} else {
232+
ClientDB.pinnedScores.add(score);
233+
}
234+
235+
this.search();
236+
}
237+
}
238+
}
201239
}

src/main/java/space/cutekitten/commandhelper/mixin/InGameHudMixin.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
import org.spongepowered.asm.mixin.injection.Redirect;
2121
import space.cutekitten.commandhelper.client.ClientDB;
2222

23-
import java.util.ArrayList;
24-
import java.util.Iterator;
25-
import java.util.List;
26-
import java.util.Objects;
23+
import java.util.*;
2724

2825
import static net.minecraft.client.gui.DrawableHelper.fill;
2926

@@ -54,6 +51,8 @@ private void customRenderScoreboardSidebar(InGameHud instance, MatrixStack matri
5451
list.remove(list.size() - 1);
5552
}
5653

54+
Collections.reverse(list);
55+
5756
List<Pair<ScoreboardPlayerScore, Text>> list2 = Lists.newArrayListWithCapacity(list.size());
5857
Text text = Text.of(ClientDB.currentSearch);
5958
int i = this.getTextRenderer().getWidth(text);
@@ -92,7 +91,8 @@ private void customRenderScoreboardSidebar(InGameHud instance, MatrixStack matri
9291
int var10001 = o - 2;
9392
Objects.requireNonNull(this.getTextRenderer());
9493
fill(matrices, var10001, t, u, t + 9, q);
95-
this.getTextRenderer().draw(matrices, text3, (float) o, (float) t, -1);
94+
this.getTextRenderer().draw(matrices, text3, (float) o, (float) t,
95+
ClientDB.pinnedScores.contains(scoreboardPlayerScore2) ? 0xFFFFFF00 : 0xFFFFFFFF);
9696
this.getTextRenderer().draw(matrices, string, (float) (u - this.getTextRenderer().getWidth(string)), (float) t, -1);
9797
if (p == list.size()) {
9898
var10001 = o - 2;

0 commit comments

Comments
 (0)