Added Config System, Verbose Logger and Pin-Map Entries

MOD REBRAND from "Minecraft Arduino Controls" to "MicrocontrollerMC" (to also include other Boards)
Introduction of Config file system
Introduction of Verbose Logger (toggle-able in config file)
Introduction of Pin-Map Entries and their Display status in config file
Enhancement of fabric.json to improve visibility in ModMenu
This commit is contained in:
Jonas_Jones 2022-06-24 23:32:05 +02:00
parent 36f0eeb701
commit 086b128551
18 changed files with 471 additions and 31 deletions

View file

@ -1,4 +1,4 @@
# MinecraftArduinoControls
# MicrocontrollerMC
A Minecraft mod that allows for Arduino Control
## This is a *very* *very* early state of this mod. Only a very small amount of the planned content for this mod has been implemented yet so basically don't even bother trying it out.

View file

@ -3,14 +3,15 @@
"id": "arduinoctrls",
"version": "v0.0.1alpha01",
"name": "Minecraft Arduino Controls",
"description": "A mod that allows for communication between the Minecraft client and an Arduino.",
"name": "MicrocontrollerMC",
"description": "A mod that allows for communication between the Minecraft client and multiple microcontrollers, allowing for ingame triggers of actions.",
"authors": [
"Jonas_Jones"
],
"contact": {
"homepage": "https://jonasjones.me/MinecraftArduinoControls",
"sources": "https://github.com/J-onasJones/MinecraftArduinoControls"
"homepage": "https://jonasjones.me/MicrocontrollerMC",
"sources": "https://github.com/J-onasJones/MicrocontrollerMC",
"issues": "https://github.com/J-onasJones/MicrocontrollerMC/issues"
},
"license": "CC0-1.0",
@ -18,7 +19,7 @@
"environment": "client",
"entrypoints": {
"main": [
"client": [
"me.jonasjones.arduinoctrls.ArduinoControls"
]
},
@ -34,5 +35,12 @@
},
"suggests": {
"another-mod": "*"
},
"custom": {
"modmenu": {
"links": {
"modmenu.discord": "https://discord.com/invite/V2EsuUVmWh"
}
}
}
}

View file

@ -1,36 +1,37 @@
package me.jonasjones.arduinoctrls;
import me.jonasjones.arduinoctrls.config.ModConfigs;
import me.jonasjones.arduinoctrls.debug.LedBlink;
import net.fabricmc.api.ModInitializer;
import me.jonasjones.arduinoctrls.util.VerboseLogger;
import net.fabricmc.api.ClientModInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ArduinoControls implements ModInitializer {
public class ArduinoControls implements ClientModInitializer {
// This logger is used to write text to the console and the log file.
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger("arduinoctrls");
public static final String MOD_ID = "arduinoctrls";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static Logger VERBOSELOGGER = LoggerFactory.getLogger(MOD_ID + " - VERBOSE LOGGER");
@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
public void onInitializeClient() {
// register configs
ModConfigs.registerConfigs();
//message from mod to confirm that it has been loaded
LOGGER.info("UwU from Arduino Controls!");
try {
//make verbose logger show that it is active
VerboseLogger.info("Verbose Logger is now logging.");
//blink LED on startup
/*try {
LedBlink.initialisation();
} catch (InterruptedException e) {
LOGGER.info("Failed to make Arduino LED blink. ARDUINO NOT CONNECTED!");
}
}*/
}
}
/*
//TODO:
- Full control over all programmable pins of all known raspberry pi's
- system to flash program onto arduino with ingame editor
- scratch system that allows for multiple event triggering when something happens
- cry because mojang UI system sucks ass
- make own library that allows for easy UI implementation
*/

View file

@ -0,0 +1,30 @@
package me.jonasjones.arduinoctrls.config;
import com.mojang.datafixers.util.Pair;
import me.jonasjones.arduinoctrls.config.SimpleConfig.DefaultConfig;
import java.util.ArrayList;
import java.util.List;
public class ModConfigProvider implements DefaultConfig {
private String configContents = "";
public List<Pair> getConfigsList() {
return configsList;
}
private final List<Pair> configsList = new ArrayList<>();
public void addKeyValuePair(Pair<String, ?> keyValuePair, String comment) {
configsList.add(keyValuePair);
configContents += keyValuePair.getFirst() + "=" + keyValuePair.getSecond() + " #"
+ comment + " | default: " + keyValuePair.getSecond() + "\n";
}
@Override
public String get(String namespace) {
return configContents;
}
}

View file

@ -0,0 +1,110 @@
package me.jonasjones.arduinoctrls.config;
import com.mojang.datafixers.util.Pair;
import me.jonasjones.arduinoctrls.ArduinoControls;
public class ModConfigs {
public static SimpleConfig CONFIGMAIN;
public static SimpleConfig CONFIGLISTS;
private static ModConfigProvider configMain;
private static ModConfigProvider configLists;
//configMain
public static Boolean ISENABLED;
public static String DEFAULTBOARDFALLBACK;
public static int LOOPINTERVAL;
public static Boolean BOARDMELTING;
public static Boolean VERBOSE = false; //needs to be set to false since the verbose logger is called before config file is fully loaded
public static String EDITOR;
//configList
public static Boolean DISPLAYENTRYALL;
public static Boolean DISPLAYENTRYPLAYERJUMP;
public static Boolean DISPLAYENTRYPLAYERSPRINT;
public static Boolean DISPLAYENTRYPLAYERWALKFORWARDS;
public static Boolean DISPLAYENTRYPLAYERWALKBACKWARDS;
public static Boolean DISPLAYENTRYPLAYERWALKRIGHT;
public static Boolean DISPLAYENTRYPLAYERWALKLEFT;
public static Boolean DISPLAYENTRYPLAYERSNEAK;
public static Boolean DISPLAYENTRYISALL;
public static Boolean DISPLAYENTRYISPLAYERJUMP;
public static Boolean DISPLAYENTRYISPLAYERSPRINT;
public static Boolean DISPLAYENTRYISPLAYERWALKFORWARDS;
public static Boolean DISPLAYENTRYISPLAYERWALKBACKWARDS;
public static Boolean DISPLAYENTRYISPLAYERWALKRIGHT;
public static Boolean DISPLAYENTRYISPLAYERWALKLEFT;
public static Boolean DISPLAYENTRYISPLAYERSNEAK;
public static void registerConfigs() {
configMain = new ModConfigProvider();
configLists = new ModConfigProvider();
createConfigs();
CONFIGMAIN = SimpleConfig.of(ArduinoControls.MOD_ID + "-main").provider(configMain).request();
CONFIGLISTS = SimpleConfig.of(ArduinoControls.MOD_ID + "-lists").provider(configLists).request();
assignConfigs();
}
private static void createConfigs() {
//configMain
configMain.addKeyValuePair(new Pair<>("basic.isEnabled", true), "whether or not the mod is enabled");
configMain.addKeyValuePair(new Pair<>("basic.defaultBoardFallback", "A-nano"), "The board to automatically be selected if failed to detect automatically. All valid ID's can be found at: https://github.com/J-onasJones/MicrocontrollerMC/wiki/Microcontroller-Boards#supported-boards");
configMain.addKeyValuePair(new Pair<>("advanced.loopInterval", 50), "The Delay in ms between each check.");
configMain.addKeyValuePair(new Pair<>("fun.boardMelting", false), "Whether or not the board should have a flame overlay instead of being greyed out when not connected.");
configMain.addKeyValuePair(new Pair<>("debug.verbose", false), "Toggle verbose console output.");
configMain.addKeyValuePair(new Pair<>("editor.default", "ingame"), "The default editor for the board program. All valid default Editors can be found at: https://github.com/J-onasJones/MicrocontrollerMC/wiki/Board-Program-Editors#available-editors");
//configLists
configLists.addKeyValuePair(new Pair<>("displayEntry.all", false), "Whether or not to display all entries. This overrides all other states except for debug and experimental entries. More infos can be found at: https://github.com/J-onasJones/MicrocontrollerMC/wiki/Pin-Map-Entries#list-of-all-entries");
configLists.addKeyValuePair(new Pair<>("displayEntry.playerJump", true), "Display Player Jump Entry");
configLists.addKeyValuePair(new Pair<>("displayEntry.playerSprint", true), "Display Player Sprint Entry");
configLists.addKeyValuePair(new Pair<>("displayEntry.playerWalkForwards", true), "Display Player Walk Forwards Entry");
configLists.addKeyValuePair(new Pair<>("displayEntry.playerWalkBackwards", true), "Display Player Walk Backwards Entry");
configLists.addKeyValuePair(new Pair<>("displayEntry.playerWalkRight", true), "Display Player Walk Right Entry");
configLists.addKeyValuePair(new Pair<>("displayEntry.playerWalkLeft", true), "Display Player Walk Left Entry");
configLists.addKeyValuePair(new Pair<>("displayEntry.playerSneak", true), "Display Player Sneak Entry");
configLists.addKeyValuePair(new Pair<>("displayEntryIs.all", false), "Whether or not to display all 'Is' entries. This overrides all other states except for debug and experimental entries. More infos can be found at: https://github.com/J-onasJones/MicrocontrollerMC/wiki/Pin-Map-Entries#list-of-all-entries");
configLists.addKeyValuePair(new Pair<>("displayEntryIs.playerJump", true), "Display Is Player Jumping Entry");
configLists.addKeyValuePair(new Pair<>("displayEntryIs.playerSprint", true), "Display Is Player Sprinting Entry");
configLists.addKeyValuePair(new Pair<>("displayEntryIs.playerWalkForwards", true), "Display Is Player Walking Forwards Entry");
configLists.addKeyValuePair(new Pair<>("displayEntryIs.playerWalkBackwards", true), "Display Is Player Walking Backwards Entry");
configLists.addKeyValuePair(new Pair<>("displayEntryIs.playerWalkRight", true), "Display Is Player Walking Right Entry");
configLists.addKeyValuePair(new Pair<>("displayEntryIs.playerWalkLeft", true), "Display Is Player Walking Left Entry");
configLists.addKeyValuePair(new Pair<>("displayEntryIs.playerSneak", true), "Display Is Player Sneaking Entry");
}
private static void assignConfigs() {
ISENABLED = CONFIGMAIN.getOrDefault("basic.isEnabled", true);
DEFAULTBOARDFALLBACK = CONFIGMAIN.getOrDefault("basic.defaultBoardFallback", "A-nano");
LOOPINTERVAL = CONFIGMAIN.getOrDefault("advanced.loopInterval", 50);
BOARDMELTING = CONFIGMAIN.getOrDefault("fun.boardMelting", false);
VERBOSE = CONFIGMAIN.getOrDefault("debug.verbose", false);
EDITOR = CONFIGMAIN.getOrDefault("editor.default", "ingame");
DISPLAYENTRYALL = CONFIGLISTS.getOrDefault("displayEntry.all", false);
DISPLAYENTRYPLAYERJUMP = CONFIGLISTS.getOrDefault("displayEntry.playerJump", true);
DISPLAYENTRYPLAYERSPRINT = CONFIGLISTS.getOrDefault("displayEntry.playerSprint", true);
DISPLAYENTRYPLAYERWALKFORWARDS = CONFIGLISTS.getOrDefault("displayEntry.playerWalkForwards", true);
DISPLAYENTRYPLAYERWALKBACKWARDS = CONFIGLISTS.getOrDefault("displayEntry.playerWalkBackwards", true);
DISPLAYENTRYPLAYERWALKRIGHT = CONFIGLISTS.getOrDefault("displayEntry.playerWalkRight", true);
DISPLAYENTRYPLAYERWALKLEFT = CONFIGLISTS.getOrDefault("displayEntry.playerWalkRight", true);
DISPLAYENTRYPLAYERSNEAK = CONFIGLISTS.getOrDefault("displayEntry.playerSneak", true);
DISPLAYENTRYISALL = CONFIGLISTS.getOrDefault("displayEntryIs.all", false);
DISPLAYENTRYISPLAYERJUMP = CONFIGLISTS.getOrDefault("displayEntryIs.playerJump", true);
DISPLAYENTRYISPLAYERSPRINT = CONFIGLISTS.getOrDefault("displayEntryIs.playerSprint", true);
DISPLAYENTRYISPLAYERWALKFORWARDS = CONFIGLISTS.getOrDefault("displayEntryIs.playerWalkForwards", true);
DISPLAYENTRYISPLAYERWALKBACKWARDS = CONFIGLISTS.getOrDefault("displayEntryIs.playerWalkBackwards", true);
DISPLAYENTRYISPLAYERWALKRIGHT = CONFIGLISTS.getOrDefault("displayEntryIs.playerWalkRight", true);
DISPLAYENTRYISPLAYERWALKLEFT = CONFIGLISTS.getOrDefault("displayEntryIs.playerWalkLeft", true);
DISPLAYENTRYISPLAYERSNEAK = CONFIGLISTS.getOrDefault("displayEntryIs.playerSneak", true);
System.out.println("All " + configMain.getConfigsList().size() + " have been set properly");
}
}

View file

@ -0,0 +1,250 @@
package me.jonasjones.arduinoctrls.config;
/*
* Copyright (c) 2021 magistermaks
* Slightly modified by Jonas_Jones 2022
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import me.jonasjones.arduinoctrls.util.VerboseLogger;
import net.fabricmc.loader.api.FabricLoader;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Scanner;
public class SimpleConfig {
private final HashMap<String, String> config = new HashMap<>();
private final ConfigRequest request;
private boolean broken = false;
public interface DefaultConfig {
String get( String namespace );
static String empty( String namespace ) {
return "";
}
}
public static class ConfigRequest {
private final File file;
private final String filename;
private DefaultConfig provider;
private ConfigRequest(File file, String filename ) {
this.file = file;
this.filename = filename;
this.provider = DefaultConfig::empty;
}
/**
* Sets the default config provider, used to generate the
* config if it's missing.
*
* @param provider default config provider
* @return current config request object
* @see DefaultConfig
*/
public ConfigRequest provider( DefaultConfig provider ) {
this.provider = provider;
return this;
}
/**
* Loads the config from the filesystem.
*
* @return config object
* @see SimpleConfig
*/
public SimpleConfig request() {
return new SimpleConfig( this );
}
private String getConfig() {
return provider.get( filename ) + "\n";
}
}
/**
* Creates new config request object, ideally `namespace`
* should be the name of the mod id of the requesting mod
*
* @param filename - name of the config file
* @return new config request object
*/
public static ConfigRequest of( String filename ) {
Path path = FabricLoader.getInstance().getConfigDir();
return new ConfigRequest( path.resolve( filename + ".properties" ).toFile(), filename );
}
private void createConfig() throws IOException {
// try creating missing files
request.file.getParentFile().mkdirs();
Files.createFile( request.file.toPath() );
// write default config data
PrintWriter writer = new PrintWriter(request.file, "UTF-8");
writer.write( request.getConfig() );
writer.close();
}
private void loadConfig() throws IOException {
Scanner reader = new Scanner( request.file );
for( int line = 1; reader.hasNextLine(); line ++ ) {
parseConfigEntry( reader.nextLine(), line );
}
}
private void parseConfigEntry( String entry, int line ) {
if( !entry.isEmpty() && !entry.startsWith( "#" ) ) {
String[] parts = entry.split("=", 2);
if( parts.length == 2 ) {
//Recognise comments after a value
String temp = parts[1].split(" #")[0];
config.put( parts[0], temp);
}else{
throw new RuntimeException("Syntax error in config file on line " + line + "!");
}
}
}
private SimpleConfig( ConfigRequest request ) {
this.request = request;
String identifier = "Config '" + request.filename + "'";
if( !request.file.exists() ) {
VerboseLogger.info( identifier + " is missing, generating default one..." );
try {
createConfig();
} catch (IOException e) {
VerboseLogger.error( identifier + " failed to generate!" );
VerboseLogger.trace(String.valueOf(e));
broken = true;
}
}
if( !broken ) {
try {
loadConfig();
} catch (Exception e) {
VerboseLogger.error( identifier + " failed to load!" );
VerboseLogger.trace(String.valueOf(e));
broken = true;
}
}
}
/**
* Queries a value from config, returns `null` if the
* key does not exist.
*
* @return value corresponding to the given key
* @see SimpleConfig#getOrDefault
*/
@Deprecated
public String get( String key ) {
return config.get( key );
}
/**
* Returns string value from config corresponding to the given
* key, or the default string if the key is missing.
*
* @return value corresponding to the given key, or the default value
*/
public String getOrDefault( String key, String def ) {
String val = get(key);
return val == null ? def : val;
}
/**
* Returns integer value from config corresponding to the given
* key, or the default integer if the key is missing or invalid.
*
* @return value corresponding to the given key, or the default value
*/
public int getOrDefault( String key, int def ) {
try {
return Integer.parseInt( get(key) );
} catch (Exception e) {
return def;
}
}
/**
* Returns boolean value from config corresponding to the given
* key, or the default boolean if the key is missing.
*
* @return value corresponding to the given key, or the default value
*/
public boolean getOrDefault( String key, boolean def ) {
String val = get(key);
if( val != null ) {
return val.equalsIgnoreCase("true");
}
return def;
}
/**
* Returns double value from config corresponding to the given
* key, or the default string if the key is missing or invalid.
*
* @return value corresponding to the given key, or the default value
*/
public double getOrDefault( String key, double def ) {
try {
return Double.parseDouble( get(key) );
} catch (Exception e) {
return def;
}
}
/**
* If any error occurred during loading or reading from the config
* a 'broken' flag is set, indicating that the config's state
* is undefined and should be discarded using `delete()`
*
* @return the 'broken' flag of the configuration
*/
public boolean isBroken() {
return broken;
}
/**
* deletes the config file from the filesystem
*
* @return true if the operation was successful
*/
public boolean delete() {
VerboseLogger.warn( "Config '" + request.filename + "' was removed from existence! Restart the game to regenerate it." );
return request.file.delete();
}
}

View file

@ -23,7 +23,7 @@ public class GuiHome extends Screen {
this.addDrawableChild(new ButtonWidget(this.width / 2 - 155, this.height / 6 + 12, 150, 20, Text.of("Configure Microcontrollers"), (button) -> {
this.client.setScreen(new SelectDevice(this));
}));
this.addDrawableChild(new ButtonWidget(this.width / 2 + 5, this.height / 6 + 12, 150, 20, Text.of("Settings"), (button) -> {
this.addDrawableChild(new ButtonWidget(this.width / 2 + 5, this.height / 6 + 12, 150, 20, Text.of("Configure MicrocontrollerMC"), (button) -> {
this.client.setScreen(new ModSettings(this));
}));

View file

@ -0,0 +1,33 @@
package me.jonasjones.arduinoctrls.util;
import jdk.jfr.StackTrace;
import me.jonasjones.arduinoctrls.ArduinoControls;
import me.jonasjones.arduinoctrls.config.ModConfigs;
public class VerboseLogger {
public static void info(String message) {
if (ModConfigs.VERBOSE) {
ArduinoControls.VERBOSELOGGER.info(message);
}
}
public void debug(String message) {
if (ModConfigs.VERBOSE) {
ArduinoControls.VERBOSELOGGER.debug(message);
}
}
public static void error(String message) {
if (ModConfigs.VERBOSE) {
ArduinoControls.VERBOSELOGGER.error(message);
}
}
public static void trace( String message) {
if (ModConfigs.VERBOSE) {
ArduinoControls.VERBOSELOGGER.trace(message);
}
}
public static void warn( String message) {
if (ModConfigs.VERBOSE) {
ArduinoControls.VERBOSELOGGER.warn(message);
}
}
}

View file

@ -3,14 +3,15 @@
"id": "arduinoctrls",
"version": "v0.0.1alpha01",
"name": "Minecraft Arduino Controls",
"description": "A mod that allows for communication between the Minecraft client and an Arduino.",
"name": "MicrocontrollerMC",
"description": "A mod that allows for communication between the Minecraft client and multiple microcontrollers, allowing for ingame triggers of actions.",
"authors": [
"Jonas_Jones"
],
"contact": {
"homepage": "https://jonasjones.me/MinecraftArduinoControls",
"sources": "https://github.com/J-onasJones/MinecraftArduinoControls"
"homepage": "https://jonasjones.me/MicrocontrollerMC",
"sources": "https://github.com/J-onasJones/MicrocontrollerMC",
"issues": "https://github.com/J-onasJones/MicrocontrollerMC/issues"
},
"license": "CC0-1.0",
@ -18,7 +19,7 @@
"environment": "client",
"entrypoints": {
"main": [
"client": [
"me.jonasjones.arduinoctrls.ArduinoControls"
]
},
@ -34,5 +35,12 @@
},
"suggests": {
"another-mod": "*"
},
"custom": {
"modmenu": {
"links": {
"modmenu.discord": "https://discord.com/invite/V2EsuUVmWh"
}
}
}
}