Rewrite Macro and MacroManager in Kotlin and fix MacroCommand bugs

This commit is contained in:
Bella 2020-05-05 11:26:26 -04:00
parent ece0e86d7b
commit 777b7d6762
No known key found for this signature in database
GPG Key ID: DBD4A6030080C8B3
6 changed files with 125 additions and 128 deletions

View File

@ -9,6 +9,7 @@ import me.zeroeightsix.kami.util.Wrapper
/**
* @author dominikaaaa
* Created by dominikaaaa on 04/05/20
*/
class MacroCommand : Command("macro", ChunkBuilder().append("key|list").append("clear|message/command").build(), "m") {
override fun call(args: Array<out String?>) {
@ -34,11 +35,10 @@ class MacroCommand : Command("macro", ChunkBuilder().append("key|list").append("
return
}
args[1] == null -> { /* message */
if (Macro.getMacrosForKey(key) == null) {
if (Macro.getMacrosForKey(key) == null || Macro.getMacrosForKey(key)?.equals("")!! || Macro.getMacrosForKey(key)?.toTypedArray()?.equals("")!!) {
sendChatMessage("'&7$rKey&f' has no macros")
return
}
// TODO: empty check doesn't work idk
sendChatMessage("'&7$rKey&f' has the following macros: ")
sendStringChatMessage(Macro.getMacrosForKey(key)?.toTypedArray(), false)
return

View File

@ -1,59 +0,0 @@
package me.zeroeightsix.kami.module;
import me.zeroeightsix.kami.KamiMod;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static me.zeroeightsix.kami.command.Command.getCommandPrefix;
import static me.zeroeightsix.kami.util.Macro.*;
import static me.zeroeightsix.kami.util.MessageSendHelper.sendKamiCommand;
import static me.zeroeightsix.kami.util.MessageSendHelper.sendServerMessage;
/**
* @author dominikaaaa
*/
public class MacroManager {
/*
* Map of all the macros.
* KeyCode, Actions
*/
public static Map<String, List<String>> macros = new LinkedHashMap<>();
/**
* Reads macros from KAMIBlueMacros.json into the macros Map
*/
public static void register() {
KamiMod.log.info("Registering macros...");
readFileToMemory();
KamiMod.log.info("Macros registered");
}
/**
* Saves macros from the macros Map into KAMIBlueMacros.json
*/
public static void saveMacros() {
KamiMod.log.info("Saving macros...");
writeMemoryToFile();
KamiMod.log.info("Macros saved");
}
/**
* Sends the message or command, depending on which one it is
* @param keyCode int keycode of the key the was pressed
*/
public static void sendMacro(int keyCode) {
List<String> macrosForThisKey = getMacrosForKey(keyCode);
if (macrosForThisKey == null) return;
for (String currentMacro : macrosForThisKey) {
if (currentMacro.startsWith(getCommandPrefix())) { // this is done instead of just sending a chat packet so it doesn't add to the chat history
sendKamiCommand(currentMacro, false); // ie, the false here
} else {
sendServerMessage(currentMacro);
}
}
}
}

View File

@ -0,0 +1,46 @@
package me.zeroeightsix.kami.module
import me.zeroeightsix.kami.KamiMod
import me.zeroeightsix.kami.command.Command
import me.zeroeightsix.kami.util.Macro
import me.zeroeightsix.kami.util.MessageSendHelper
/**
* @author dominikaaaa
* Created by dominikaaaa on 04/05/20
*/
object MacroManager {
/**
* Reads macros from KAMIBlueMacros.json into the macros Map
*/
fun registerMacros() {
KamiMod.log.info("Registering macros...")
Macro.readFileToMemory()
KamiMod.log.info("Macros registered")
}
/**
* Saves macros from the macros Map into KAMIBlueMacros.json
*/
fun saveMacros() {
KamiMod.log.info("Saving macros...")
Macro.writeMemoryToFile()
KamiMod.log.info("Macros saved")
}
/**
* Sends the message or command, depending on which one it is
* @param keyCode int keycode of the key the was pressed
*/
fun sendMacro(keyCode: Int) {
val macrosForThisKey = Macro.getMacrosForKey(keyCode) ?: return
for (currentMacro in macrosForThisKey) {
if (currentMacro!!.startsWith(Command.getCommandPrefix())) { // this is done instead of just sending a chat packet so it doesn't add to the chat history
MessageSendHelper.sendKamiCommand(currentMacro, false) // ie, the false here
} else {
MessageSendHelper.sendServerMessage(currentMacro)
}
}
}
}

View File

@ -0,0 +1,17 @@
package me.zeroeightsix.kami.module;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @author dominikaaaa
* Lazy fix used for Java instance of {@link me.zeroeightsix.kami.util.Macro} and {@link MacroManager}
*/
public class Macros {
/*
* Map of all the macros.
* KeyCode, Actions
*/
public static Map<String, List<String>> macros = new LinkedHashMap<>();
}

View File

@ -1,67 +0,0 @@
package me.zeroeightsix.kami.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import me.zeroeightsix.kami.KamiMod;
import java.io.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import static me.zeroeightsix.kami.module.MacroManager.macros;
/**
* @author dominikaaaa
* TODO: rewrite in Kotlin once tested and working
* TODO: register getMacrosAsArray() in {@link me.zeroeightsix.kami.event.ForgeEventProcessor}
*/
public class Macro {
private static Gson gson = new GsonBuilder().create();
public static final String CONFIG_NAME = "KAMIBlueMacros.json";
private static File file = new File(CONFIG_NAME);
public static void writeMemoryToFile() {
try {
FileWriter fw = new FileWriter(file, false);
gson.toJson(macros, fw);
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readFileToMemory() {
try {
macros = gson.fromJson(new FileReader(file), new TypeToken<HashMap<String, List<String>>>() {}.getType());
} catch (FileNotFoundException e) {
KamiMod.log.warn("Could not find file " + CONFIG_NAME + ", clearing the macros list");
macros.clear();
}
}
public static List<String> getMacrosForKey(int keyCode) {
for (Map.Entry<String, List<String>> entry : macros.entrySet()) {
if (keyCode == Integer.parseInt(entry.getKey())) {
return entry.getValue();
}
}
return null;
}
public static void addMacroToKey(String keyCode, String macro) {
if (macro == null) return; // prevent trying to add a null macro
macros.computeIfAbsent(keyCode, (key) -> new LinkedList()).add(macro);
}
public static void removeMacro(String keyCode) {
for (Map.Entry<String, List<String>> entry : macros.entrySet()) {
if (entry.getKey().equals(keyCode)) {
entry.setValue(null);
}
}
}
}

View File

@ -0,0 +1,60 @@
package me.zeroeightsix.kami.util
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import me.zeroeightsix.kami.KamiMod
import me.zeroeightsix.kami.module.Macros
import java.io.*
import java.util.*
/**
* @author dominikaaaa
* Created by dominikaaaa on 04/05/20
*/
object Macro {
private val gson = GsonBuilder().create()
private const val configName = "KAMIBlueMacros.json"
private val file = File(configName)
fun writeMemoryToFile() {
try {
val fw = FileWriter(file, false)
gson.toJson(Macros.macros, fw)
fw.flush()
fw.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
fun readFileToMemory() {
try {
Macros.macros = gson.fromJson(FileReader(file), object : TypeToken<HashMap<String?, List<String?>?>?>() {}.type)
} catch (e: FileNotFoundException) {
KamiMod.log.warn("Could not find file $configName, clearing the macros list")
Macros.macros.clear()
}
}
fun getMacrosForKey(keyCode: Int): List<String?>? {
for ((key, value) in Macros.macros) {
if (keyCode == key.toInt()) {
return value
}
}
return null
}
fun addMacroToKey(keyCode: String?, macro: String?) {
if (macro == null) return // prevent trying to add a null macro
Macros.macros.getOrPut(keyCode, ::mutableListOf).add(macro)
}
fun removeMacro(keyCode: String) {
for (entry in Macros.macros.entries) {
if (entry.key == keyCode) {
entry.setValue(null)
}
}
}
}