aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/example/CrashCommand.java
blob: 4fb9cbbea8faa6861dd53cc75304a602a904a070 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.example;

import net.minecraft.client.Minecraft;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.scoreboard.ScoreObjective;
import net.minecraft.scoreboard.ScorePlayerTeam;
import net.minecraft.scoreboard.Scoreboard;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentText;
import org.apache.logging.log4j.LogManager;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

public class CrashCommand extends CommandBase {
    @Override
    public String getCommandName() {
        return "randomutils";
    }

    @Override
    public String getCommandUsage(ICommandSender sender) {
        return "";
    }

    @Override
    public void processCommand(ICommandSender sender, String[] args) throws CommandException {
        if (args.length == 0) {
            sender.addChatMessage(new ChatComponentText("§cPlease use an argument"));
        } else if (args[0].equals("weather")) {
            sender.addChatMessage(new ChatComponentText("§bCurrent Weather: " +
                    (Minecraft.getMinecraft().theWorld.isRaining() ? "§7Rainy!" : "§eSunny!")));
        } else if (args[0].equals("coinflip")) {
            sender.addChatMessage(new ChatComponentText("§bCoinflip: " +
                    (ThreadLocalRandom.current().nextBoolean() ? "§eHeads" : "§eTails")));
        } else if (args[0].equals("scoreboard")) {
            final int SIDEBAR_SLOT = 1;

            Scoreboard scoreboard = Minecraft.getMinecraft().theWorld.getScoreboard();
            ScoreObjective objective = scoreboard.getObjectiveInDisplaySlot(SIDEBAR_SLOT);
            List<String> scoreList = scoreboard.getSortedScores(objective)
                    .stream()
                    .limit(15)
                    .map(score ->
                            ScorePlayerTeam.formatPlayerName(
                                    scoreboard.getPlayersTeam(score.getPlayerName()),
                                    score.getPlayerName()))
                    .map(text -> {
                        StringBuilder sb = new StringBuilder();
                        for (char c : text.toCharArray()) {
                            if (Minecraft.getMinecraft().fontRendererObj.getCharWidth(c) > 0 || c == '§')
                                sb.append(c);
                        }
                        return sb.toString();
                    })
                    .collect(Collectors.toList());
            Collections.reverse(scoreList);
            for (String s : scoreList) {
                LogManager.getLogger("Scoreboard").info(s);
                sender.addChatMessage(new ChatComponentText(s));
            }
        } else {
            sender.addChatMessage(new ChatComponentText("§cUnknown subcommand"));
        }
    }

    @Override
    public List<String> addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) {
        if (args.length == 1)
            return getListOfStringsMatchingLastWord(args, "weather", "coinflip", "scoreboard");
        return Arrays.asList();
    }

    @Override
    public boolean canCommandSenderUseCommand(ICommandSender sender) {
        return true;
    }
}