aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/neodymium/command/NeodymiumCommand.java
blob: 3040ba95be1d405641a536bab8f424281c691598 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package makamys.neodymium.command;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import org.apache.commons.lang3.tuple.Pair;
import org.lwjgl.input.Mouse;

import makamys.neodymium.Compat;
import makamys.neodymium.Compat.Warning;
import makamys.neodymium.Neodymium;
import makamys.neodymium.util.ChatUtil;
import makamys.neodymium.util.ChatUtil.MessageVerbosity;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.event.ClickEvent;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.ClientCommandHandler;

public class NeodymiumCommand extends CommandBase {
    
    public static final EnumChatFormatting HELP_COLOR = EnumChatFormatting.BLUE;
    public static final EnumChatFormatting HELP_USAGE_COLOR = EnumChatFormatting.YELLOW;
    public static final EnumChatFormatting HELP_WARNING_COLOR = EnumChatFormatting.YELLOW;
    public static final EnumChatFormatting HELP_EMPHASIS_COLOR = EnumChatFormatting.DARK_AQUA;
    public static final EnumChatFormatting ERROR_COLOR = EnumChatFormatting.RED;
    
    private static Map<String, ISubCommand> subCommands = new HashMap<>();
    
    public static void init() {
        ClientCommandHandler.instance.registerCommand(new NeodymiumCommand());
        registerSubCommand("status", new StatusCommand());
        registerSubCommand("disable_advanced_opengl", new DisableAdvancedOpenGLCommand());
    }
    
    public static void registerSubCommand(String key, ISubCommand command) {
        subCommands.put(key, command);   
    }
    
    @Override
    public String getCommandName() {
        return "neodymium";
    }

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

    @Override
    public void processCommand(ICommandSender sender, String[] args) {
        if(args.length > 0) {
            ISubCommand subCommand = subCommands.get(args[0]);
            if(subCommand != null) {
                subCommand.processCommand(sender, args);
                return;
            }
        }
        throw new WrongUsageException(getCommandName() + " <" + String.join("|", getNonSecretSubCommands()) + ">", new Object[0]);
    }
    
    private String[] getNonSecretSubCommands() {
        return subCommands.entrySet()
                .stream()
                .filter(e -> !e.getValue().isSecret())
                .map(e -> e.getKey()).toArray(String[]::new);
    }
    
    public static void addChatMessage(ICommandSender sender, String text) {
        sender.addChatMessage(new ChatComponentText(text));
    }
    
    public static void addColoredChatMessage(ICommandSender sender, String text, EnumChatFormatting color) {
        addColoredChatMessage(sender, text, color, null);
    }
    
    public static void addColoredChatMessage(ICommandSender sender, String text, EnumChatFormatting color, Consumer<ChatComponentText> fixup) {
        ChatComponentText msg = new ChatComponentText(text);
        msg.getChatStyle().setColor(color);
        if(fixup != null) {
            fixup.accept(msg);
        }
        sender.addChatMessage(msg);
    }
    
    @SuppressWarnings("unchecked")
    public List<String> addTabCompletionOptions(ICommandSender sender, String[] args) {
        return args.length == 1 ? getListOfStringsMatchingLastWord(args, getNonSecretSubCommands()) : null;
    }
    
    public static class StatusCommand implements ISubCommand {

        @Override
        public void processCommand(ICommandSender sender, String[] args) {
            if(Neodymium.renderer != null) {
                List<String> text = Neodymium.renderer.getDebugText(true);
                addChatMessages(sender, text);
            }
            Pair<List<Warning>, List<Warning>> allWarns = Neodymium.showCompatStatus(true);
            List<Warning> warns = allWarns.getLeft();
            List<Warning> criticalWarns = allWarns.getRight();
            for(Warning line : warns) {
                addColoredChatMessageWithAction(sender, "* " + line.text, HELP_WARNING_COLOR, line.action);
            }
            for(Warning line : criticalWarns) {
                addColoredChatMessageWithAction(sender, "* " + line.text, ERROR_COLOR, line.action);
            }
        }
        
        private void addColoredChatMessageWithAction(ICommandSender sender, String text, EnumChatFormatting color, Runnable action) {
            ChatComponentText msg = new ChatComponentText(text);
            msg.getChatStyle().setColor(color);
            msg.getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "neodymium disable_advanced_opengl"));
            sender.addChatMessage(msg);
        }

        private static void addChatMessages(ICommandSender sender, Collection<String> messages) {
            for(String line : messages) {
                sender.addChatMessage(new ChatComponentText(line));
            }
        }
        
    }
    
    public static class DisableAdvancedOpenGLCommand implements ISubCommand {

        @Override
        public void processCommand(ICommandSender sender, String[] args) {
            Minecraft mc = Minecraft.getMinecraft();
            if(Compat.disableAdvancedOpenGL()) {
                mc.getSoundHandler().playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation("gui.button.press"), 1.0F));
                ChatUtil.showNeoChatMessage(EnumChatFormatting.AQUA + "Disabled Advanced OpenGL.", MessageVerbosity.INFO);
                mc.renderGlobal.loadRenderers();
            } else {
                float x = Mouse.getX() / (float)Minecraft.getMinecraft().displayWidth;
                mc.getSoundHandler().playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation("note.bass"), 0.7F + x * 0.2F));
            }
        }
        
        @Override
        public boolean isSecret() {
            return true;
        }
        
    }
    
}