From 7ed302d086bb7ec229f39dcbcb3c3370253404b4 Mon Sep 17 00:00:00 2001 From: bowser0000 Date: Mon, 6 Jul 2020 22:32:58 -0400 Subject: Add API key and config Added /setkey [key] and /getkey commands. Created config to store API key and toggled features. --- me/Danker/ConfigHandler.java | 120 +++++++++++++++++++++++++++++++++++++++++++ me/Danker/GetkeyCommand.java | 39 ++++++++++++++ me/Danker/SetkeyCommand.java | 41 +++++++++++++++ me/Danker/TheMod.java | 14 ++++- me/Danker/ToggleCommand.java | 12 ++++- 5 files changed, 223 insertions(+), 3 deletions(-) create mode 100644 me/Danker/ConfigHandler.java create mode 100644 me/Danker/GetkeyCommand.java create mode 100644 me/Danker/SetkeyCommand.java diff --git a/me/Danker/ConfigHandler.java b/me/Danker/ConfigHandler.java new file mode 100644 index 0000000..0399588 --- /dev/null +++ b/me/Danker/ConfigHandler.java @@ -0,0 +1,120 @@ +package me.Danker; + +import java.io.File; + +import net.minecraftforge.common.config.Configuration; + +public class ConfigHandler { + public static Configuration config; + private static String file = "config/Danker's Skyblock Mod.cfg"; + + public static void init() { + config = new Configuration(new File(file)); + try { + config.load(); + } catch (Exception ex) { + System.err.print(ex); + } finally { + config.save(); + } + } + + public static int getInt(String category, String key) { + config = new Configuration(new File(file)); + try { + config.load(); + if (config.getCategory(category).containsKey(key)) { + return config.get(category, key, 0).getInt(); + } + } catch (Exception ex) { + System.err.print(ex); + } finally { + config.save(); + } + return 0; + } + + public static String getString(String category, String key) { + config = new Configuration(new File(file)); + try { + config.load(); + if (config.getCategory(category).containsKey(key)) { + return config.get(category, key, "").getString(); + } + } catch (Exception ex) { + System.err.print(ex); + } finally { + config.save(); + } + return ""; + } + + public static boolean getBoolean(String category, String key) { + config = new Configuration(new File(file)); + try { + config.load(); + if (config.getCategory(category).containsKey(key)) { + return config.get(category, key, false).getBoolean(); + } + } catch (Exception ex) { + System.err.print(ex); + } finally { + config.save(); + } + return true; + } + + public static void writeIntConfig(String category, String key, int value) { + config = new Configuration(new File(file)); + try { + config.load(); + int set = config.get(category, key, value).getInt(); + config.getCategory(category).get(key).set(value); + } catch (Exception ex) { + System.err.print(ex); + } finally { + config.save(); + } + } + + public static void writeStringConfig(String category, String key, String value) { + config = new Configuration(new File(file)); + try { + config.load(); + String set = config.get(category, key, value).getString(); + config.getCategory(category).get(key).set(value); + } catch (Exception ex) { + System.err.print(ex); + } finally { + config.save(); + } + } + + public static void writeBooleanConfig(String category, String key, boolean value) { + config = new Configuration(new File(file)); + try { + config.load(); + boolean set = config.get(category, key, value).getBoolean(); + config.getCategory(category).get(key).set(value); + } catch (Exception ex) { + System.err.print(ex); + } finally { + config.save(); + } + } + + public static boolean hasKey(String category, String key) { + config = new Configuration(new File(file)); + try { + config.load(); + if (!config.hasCategory(category)) return false; + return config.getCategory(category).containsKey(key); + } catch (Exception ex) { + System.err.print(ex); + } finally { + config.save(); + } + return false; + } + +} diff --git a/me/Danker/GetkeyCommand.java b/me/Danker/GetkeyCommand.java new file mode 100644 index 0000000..60c5eb8 --- /dev/null +++ b/me/Danker/GetkeyCommand.java @@ -0,0 +1,39 @@ +package me.Danker; + +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommand; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.ChatComponentText; + +public class GetkeyCommand extends CommandBase implements ICommand { + + @Override + public String getCommandName() { + return "getkey"; + } + + @Override + public String getCommandUsage(ICommandSender arg0) { + return getCommandName(); + } + + @Override + public int getRequiredPermissionLevel() { + return 0; + } + + @Override + public void processCommand(ICommandSender arg0, String[] arg1) throws CommandException { + final EntityPlayer player = (EntityPlayer)arg0; + final ConfigHandler cf = new ConfigHandler(); + + if (cf.getString("api", "APIKey").equals("")) { + player.addChatMessage(new ChatComponentText("API key not set. Get a new one by doing /api new.")); + } else { + player.addChatMessage(new ChatComponentText("Your set API key is " + cf.getString("api", "APIKey"))); + } + } + +} diff --git a/me/Danker/SetkeyCommand.java b/me/Danker/SetkeyCommand.java new file mode 100644 index 0000000..6490611 --- /dev/null +++ b/me/Danker/SetkeyCommand.java @@ -0,0 +1,41 @@ +package me.Danker; + +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommand; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.ChatComponentText; + +public class SetkeyCommand extends CommandBase implements ICommand { + + @Override + public String getCommandName() { + return "setkey"; + } + + @Override + public String getCommandUsage(ICommandSender arg0) { + return getCommandName() + " [key]"; + } + + @Override + public int getRequiredPermissionLevel() { + return 0; + } + + @Override + public void processCommand(ICommandSender arg0, String[] arg1) throws CommandException { + final EntityPlayer player = (EntityPlayer)arg0; + + if (arg1.length == 0) { + player.addChatMessage(new ChatComponentText("Usage: /setkey [key]")); + return; + } + + final ConfigHandler cf = new ConfigHandler(); + cf.writeStringConfig("api", "APIKey", arg1[0]); + player.addChatMessage(new ChatComponentText("Set API key to " + arg1[0])); + } + +} diff --git a/me/Danker/TheMod.java b/me/Danker/TheMod.java index 3e9aa3f..5f98066 100644 --- a/me/Danker/TheMod.java +++ b/me/Danker/TheMod.java @@ -22,18 +22,30 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; public class TheMod { public static final String MODID = "Danker's Skyblock Mod"; - public static final String VERSION = "1.1"; + public static final String VERSION = "1.2"; @EventHandler public void init(FMLInitializationEvent event) { FMLCommonHandler.instance().bus().register(this); MinecraftForge.EVENT_BUS.register(this); + + // Config init + final ConfigHandler cf = new ConfigHandler(); + if (!cf.hasKey("toggles", "GParty")) cf.writeBooleanConfig("toggles", "GParty", true); + if (!cf.hasKey("toggles", "Coords")) cf.writeBooleanConfig("toggles", "Coords", true); + if (!cf.hasKey("api", "APIKey")) cf.writeStringConfig("api", "APIKey", ""); + + final ToggleCommand tf = new ToggleCommand(); + tf.gpartyToggled = cf.getBoolean("toggles", "GParty"); + tf.coordsToggled = cf.getBoolean("toggles", "Coords"); } @EventHandler public void preInit(final FMLPreInitializationEvent event) { ClientCommandHandler.instance.registerCommand(new ToggleCommand()); + ClientCommandHandler.instance.registerCommand(new SetkeyCommand()); + ClientCommandHandler.instance.registerCommand(new GetkeyCommand()); } @SubscribeEvent diff --git a/me/Danker/ToggleCommand.java b/me/Danker/ToggleCommand.java index d24c399..8c27da6 100644 --- a/me/Danker/ToggleCommand.java +++ b/me/Danker/ToggleCommand.java @@ -8,8 +8,8 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.ChatComponentText; public class ToggleCommand extends CommandBase implements ICommand { - private static boolean gpartyToggled = true; - private static boolean coordsToggled = true; + public static boolean gpartyToggled; + public static boolean coordsToggled; public boolean getToggle(String type) { if (type.equals("gparty")) { @@ -38,6 +38,7 @@ public class ToggleCommand extends CommandBase implements ICommand { @Override public void processCommand(ICommandSender arg0, String[] arg1) throws CommandException { final EntityPlayer player = (EntityPlayer)arg0; + final ConfigHandler cf = new ConfigHandler(); if (arg1.length == 0) { player.addChatMessage(new ChatComponentText("Usage: /toggle [gparty/coords]")); @@ -46,10 +47,17 @@ public class ToggleCommand extends CommandBase implements ICommand { if (arg1[0].equalsIgnoreCase("gparty")) { gpartyToggled = !gpartyToggled; + cf.writeBooleanConfig("toggles", "GParty", gpartyToggled); player.addChatMessage(new ChatComponentText("Guild party notifications has been set to " + gpartyToggled + ".")); } else if (arg1[0].equalsIgnoreCase("coords")) { coordsToggled = !coordsToggled; + cf.writeBooleanConfig("toggles", "Coords", coordsToggled); player.addChatMessage(new ChatComponentText("Coord/Angle display has been set to " + coordsToggled + ".")); + } else if (arg1[0].equalsIgnoreCase("list")) { + player.addChatMessage(new ChatComponentText("Guild party notifications: " + gpartyToggled)); + player.addChatMessage(new ChatComponentText("Coord/Angle display: " + coordsToggled)); + } else { + player.addChatMessage(new ChatComponentText("Unknown value.")); } } } -- cgit