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 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 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; } }