Skip to content

Commit

Permalink
Add set key & disable autokey command
Browse files Browse the repository at this point in the history
  • Loading branch information
udu3324 committed Oct 18, 2022
1 parent b7f5cbe commit 5d78041
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 40 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ repositories {
maven { url 'https://repo.hypixel.net/repository/Hypixel/' }
}

version = "1.4.1"
version = "1.5"
group= "com.udu3324.hytools" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "Hytools"

Expand Down
75 changes: 57 additions & 18 deletions src/main/java/com/udu3324/hytools/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;

import com.udu3324.hytools.hyapi.HypixelApiKey;

public class Command extends CommandBase {

private HashMap<String, String> collection = new HashMap<String, String>();
Expand All @@ -30,18 +32,21 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
collection.put("partyguessguilds", "3");
collection.put("nickalert", "4");
collection.put("nickalerthypixelapi", "5");
collection.put("setapikey", "6");
collection.put("autofetchapikey", "7");

// /hytools
if (args.length < 1) {
sender.addChatMessage(new ChatComponentText("\n" + EnumChatFormatting.GOLD + "[+]= Hytools v" + Reference.VERSION + " by udu3324 =[+]\n"
+ EnumChatFormatting.GOLD + "Check out my GitHub! https://github.com/udu3324 \n\n"
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + "[+]= Hytools v" + Reference.VERSION + " by udu3324 =[+]\n\n"
+ EnumChatFormatting.GREEN + "/fcheck (player1) (player2) - checks player's friend\n"
+ EnumChatFormatting.DARK_AQUA + "/hytools partyguess [toggle/on/off] [toggled|" + Config.getPartyGuess() + "]\n"
+ EnumChatFormatting.DARK_GREEN + "/hytools partyguessfriends [toggle/on/off] [toggled|" + Config.getPartyGuessFriend() +"] \n"
+ EnumChatFormatting.GOLD + "/hytools partyguessguilds [toggle/on/off] [toggled|" + Config.getPartyGuessGuild() + "]\n"
+ EnumChatFormatting.DARK_PURPLE + "/hytools nickalert [toggle/on/off] [toggled|" + Config.getNickAlert() + "]\n"
+ EnumChatFormatting.RED + "/hytools setAPIKey - Sets the api key manually\n"
+ EnumChatFormatting.RED + "/hytools autoFetchAPIKey [toggled|" + Config.getAutoFetchAPIKey() + "]\n"
+ EnumChatFormatting.DARK_AQUA + "/hytools partyguess [toggled|" + Config.getPartyGuess() + "]\n"
+ EnumChatFormatting.DARK_GREEN + "/hytools partyguessfriends [toggled|" + Config.getPartyGuessFriend() +"] \n"
+ EnumChatFormatting.GOLD + "/hytools partyguessguilds [toggled|" + Config.getPartyGuessGuild() + "]\n"
+ EnumChatFormatting.DARK_PURPLE + "/hytools nickalert [toggled|" + Config.getNickAlert() + "]\n"
+ EnumChatFormatting.GRAY + "Using Hypixel API for NickAlert is in the grey area of being allowed. " + EnumChatFormatting.BOLD + "Use at your own risk.\n"
+ EnumChatFormatting.DARK_PURPLE + "/hytools nickalerthypixelapi [toggle/on/off] [toggled|" + Config.getNickAlertHypixelAPI() + "]"));
+ EnumChatFormatting.DARK_PURPLE + "/hytools nickalerthypixelapi [toggled|" + Config.getNickAlertHypixelAPI() + "]"));
return;
}

Expand Down Expand Up @@ -75,37 +80,51 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
data = "ON";
else
data = "OFF";
} else if (command.equals("AUTOFETCHAPIKEY")) {
if (Config.getAutoFetchAPIKey())
data = "ON";
else
data = "OFF";
} else if (command.equals("SETAPIKEY")) {
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "You are missing the api key in this command!"));
return;
} else {
sender.addChatMessage(new ChatComponentText("\n" + EnumChatFormatting.RED + command +
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + command +
" doesn't exist in Hytools! "));
return;
}

sender.addChatMessage(new ChatComponentText("\n" + EnumChatFormatting.GREEN + command +
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GREEN + command +
" is currently set to " + data + "."));
return;
}

// /hytools ??? ??? (toggle a tool)
if (args.length == 2) {
String command = args[0].toLowerCase();
String data = args[1].toLowerCase();
String data = args[1].toUpperCase();
String collec = collection.get(command);

if (collec == null) {
sender.addChatMessage(new ChatComponentText(
EnumChatFormatting.RED + command + " is not a valid command. Do /hytools for help!"));
return;
}

command = command.toUpperCase();

// handle true or false as toggling
if (data.equals("TRUE")) {
data = "ON";
} else if (data.equals("FALSE"))
data = "OFF";

if (!data.equals("toggle") && !data.equals("on") && !data.equals("off")) {
if (!data.equals("TOGGLE") && !data.equals("ON") && !data.equals("OFF") && !command.equals("SETAPIKEY")) {
sender.addChatMessage(new ChatComponentText(
EnumChatFormatting.RED + "Invalid 2nd argument! Make sure its either toggle, off, or on."));
EnumChatFormatting.RED + "Invalid 2nd argument! Make sure its either toggle, on, or off."));
return;
}

command = command.toUpperCase();
data = data.toUpperCase();

if (command.equals("PARTYGUESS")) {
if (data.equals("ON")) {
Config.setPartyGuess(true);
Expand Down Expand Up @@ -176,16 +195,36 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
data = "ON";
}
}
}
} else if (command.equals("AUTOFETCHAPIKEY")) {
if (data.equals("ON")) {
Config.setAutoFetchAPIKey(true);
} else if (data.equals("OFF")) {
Config.setAutoFetchAPIKey(false);
} else if (data.equals("TOGGLE")) {
if (Config.getAutoFetchAPIKey()) {
Config.setAutoFetchAPIKey(false);
data = "OFF";
} else { //would be off
Config.setAutoFetchAPIKey(true);
data = "ON";
}
}
} else if (command.equals("SETAPIKEY")) {
if (HypixelApiKey.setKey(data, false)) {
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GREEN + "The Hypixel API key has been set sucessfully for Hytools."));
} else {
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "The Hypixel API key provided is invalid!"));
}
}

//commands that are successful end up here
sender.addChatMessage(new ChatComponentText(
EnumChatFormatting.GREEN + command + " is now set to " + data + "."));
EnumChatFormatting.GREEN + command + " is now set to " + data.toLowerCase() + "."));
}

// /hytools ??? ??? ??? (invalid command)
if (args.length >= 3) {
sender.addChatMessage(new ChatComponentText("\n" + EnumChatFormatting.RED + "Too many arguments! You probably have a typo."));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Too many arguments! You probably have a typo."));
return;
}

Expand Down
68 changes: 51 additions & 17 deletions src/main/java/com/udu3324/hytools/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.io.FileWriter;
import java.io.IOException;

import java.util.ArrayList;

import net.minecraftforge.fml.common.Loader;

public class Config {
Expand Down Expand Up @@ -50,26 +52,44 @@ public static String getValueFromConfig(String value) {
//example: setValueFromConfig("api-key", "thisIsTheApiKey")
public static void setValueFromConfig(String value, String data) {
try {
// input the (modified) file content to the StringBuffer "input"
BufferedReader file = new BufferedReader(new FileReader(configFile));
StringBuffer inputBuffer = new StringBuffer();
String line;
// get the lines in the config
BufferedReader bufferedReader = new BufferedReader(new FileReader(configFile));

while ((line = file.readLine()) != null) {
if (line.contains(value)) {
line = value + ": " + data;
}
inputBuffer.append(line);
inputBuffer.append('\n');
}
file.close();
ArrayList<String> lines = new ArrayList<String>();

String l;
while ((l = bufferedReader.readLine()) != null) {
lines.add(l);
}
bufferedReader.close();

// modify the values from the config
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);

// dont parse comments
if (!line.contains("#") && line.length() > 1 && line.contains(":")) {
// if line (value: data) -> substring to : (value) -> equals value
if (value.equals(line.substring(0, line.indexOf(":")))) {
// its the line being modified
lines.set(i, value + ": " + data);
}
}
}

// write the lines to the config
configFile.createNewFile();

// write the new string with the replaced line OVER the same file
FileOutputStream fileOut = new FileOutputStream(configFile);
fileOut.write(inputBuffer.toString().getBytes());
fileOut.close();
FileWriter writer = new FileWriter(configFile);

//write array back to new file
for (int i = 0; i < lines.size(); i++) {
writer.write(lines.get(i) + System.lineSeparator());
}

writer.close();
} catch (Exception e) {
Hytools.log.info("Problem writing file.");
Hytools.log.info("Problem writing file. " + e);
}
}

Expand All @@ -92,6 +112,7 @@ public static void create() {
w.write(System.lineSeparator());
w.write("# Hypixel API Key" + System.lineSeparator());
w.write("api-key: empty" + System.lineSeparator());
w.write("auto-fetch-api-key: true" + System.lineSeparator());
w.write(System.lineSeparator());
w.write("# Party Guess Config" + System.lineSeparator());
w.write("party-guess_toggled: true" + System.lineSeparator());
Expand Down Expand Up @@ -133,6 +154,19 @@ public static String getStoredAPIKey() {
public static void setStoredAPIKey(String key) {
setValueFromConfig("api-key", key);
}

public static Boolean getAutoFetchAPIKey() {
String str = getValueFromConfig("auto-fetch-api-key");
if (str.equals("true")) {
return true;
} else {
return false;
}
}

public static void setAutoFetchAPIKey(Boolean bool) {
setValueFromConfig("auto-fetch-api-key", String.valueOf(bool));
}

// party guess
public static Boolean getPartyGuess() {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/udu3324/hytools/Hytools.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ public void onPlayerChat(ClientChatReceivedEvent event) {
if (event.message.getFormattedText().contains("\u00A7k"))
return;

log.info(Config.getAutoFetchAPIKey());

//request for new api key
if (requestApiKeyOnce && !configAPIKeySet) {
if (Config.getAutoFetchAPIKey() && requestApiKeyOnce && !configAPIKeySet) {
Minecraft.getMinecraft().thePlayer.sendChatMessage("/api new");
requestApiKeyOnce = false;
}

final String filtered = event.message.getUnformattedText();
String filtered = event.message.getUnformattedText();

//recieve messages from command "/api new" and parse
Matcher m = Pattern.compile("^Your new API key is ").matcher(filtered);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/udu3324/hytools/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
public class Reference {
public static final String MODID = "Hytools";
public static final String NAME = "Hytools";
public static final String VERSION = "1.4.1";
public static final String VERSION = "1.5";
}
3 changes: 2 additions & 1 deletion src/main/java/com/udu3324/hytools/hyapi/HypixelApiKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public static Boolean setKey(String key, boolean sendMessageInChat) {
//if response code is not 200 (ok) then the api key is not set correctly
if (responseCode != 200) {
Hytools.log.info("HypixelApiKey.java | Not a valid API key!");
Hytools.sendMessage("\u00A74\u00A7lERROR! API key from /api new did not work.");
if (sendMessageInChat)
Hytools.sendMessage("\u00A74\u00A7lERROR! API key from /api new did not work.");
apiKeySet = false;
return false;
}
Expand Down

4 comments on commit 5d78041

@buiawpkgew1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add i18n?

@udu3324
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@buiawpkgew1

Can you add i18n?

? I don't understand

@buiawpkgew1
Copy link
Contributor

@buiawpkgew1 buiawpkgew1 commented on 5d78041 Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

translate

@afkvido
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@udu3324 i18n means to internationalize your software by adding many languages and translations. What @buiawpkgew1 meant in his first comment is known as "please make a crowdin for Hytools"

Please sign in to comment.