Skip to content

Commit

Permalink
Implement config menu.
Browse files Browse the repository at this point in the history
  • Loading branch information
Provismet committed Oct 11, 2023
1 parent 8c8e94d commit 24a8389
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/provismet/vmcmc/ClientVMC.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static Identifier identifier (String path) {

@Override
public void onInitializeClient () {
Pair<String,Integer> portInfo = Config.getPortInfo();
Pair<String,Integer> portInfo = Config.readJSON();
PacketSender.initPort(portInfo.getLeft(), portInfo.getRight());
CaptureRegistry.registerStandardEvents();

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/provismet/vmcmc/config/ClothVMC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.provismet.vmcmc.config;

import com.provismet.vmcmc.vmc.PacketSender;

import me.shedaniel.clothconfig2.api.ConfigBuilder;
import me.shedaniel.clothconfig2.api.ConfigCategory;
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;

public class ClothVMC {
public static Screen build (Screen parent) {
ConfigBuilder builder = ConfigBuilder.create();
builder.setParentScreen(parent);
builder.setTitle(Text.translatable("title.vmcmc.config"));

ConfigCategory general = builder.getOrCreateCategory(Text.translatable("category.vmcmc.general"));
ConfigEntryBuilder entryBuilder = builder.entryBuilder();

general.addEntry(entryBuilder.startStrField(Text.translatable("entry.vmcmc.general.ip"), Config.getIP())
.setDefaultValue(PacketSender.LOCALHOST)
.setTooltip(Text.translatable("tooltip.vmcmc.general.ip"))
.setSaveConsumer(newValue -> Config.setIP(newValue))
.build()
);

general.addEntry(entryBuilder.startIntField(Text.translatable("entry.vmcmc.general.port"), Config.getPort())
.setDefaultValue(PacketSender.DEFAULT_PORT)
.setTooltip(Text.translatable("tooltip.vmcmc.general.port"))
.setSaveConsumer(newValue -> Config.setPort(newValue))
.build()
);

builder.setSavingRunnable(() -> {
Config.saveJSON();
PacketSender.initPort(Config.getIP(), Config.getPort());
});

return builder.build();
}
}
61 changes: 45 additions & 16 deletions src/main/java/com/provismet/vmcmc/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.FileWriter;

import com.google.gson.stream.JsonReader;
import com.provismet.vmcmc.ClientVMC;
import com.provismet.vmcmc.vmc.PacketSender;

import net.minecraft.util.Pair;
Expand All @@ -14,14 +15,15 @@ public class Config {
private static final String PORT = "port";
private static final String FILEPATH = "config/vmc-mc.json";

public static Pair<String,Integer> getPortInfo () {
private static String host = PacketSender.LOCALHOST;
private static int port = PacketSender.DEFAULT_PORT;

public static Pair<String,Integer> readJSON () {
try {
FileReader reader = new FileReader(FILEPATH);
JsonReader parser = new JsonReader(reader);

String host = PacketSender.LOCALHOST;
int port = PacketSender.DEFAULT_PORT;

parser.beginObject();
while (parser.hasNext()) {
final String name = parser.nextName();
switch (name) {
Expand All @@ -41,22 +43,49 @@ public static Pair<String,Integer> getPortInfo () {
return new Pair<>(host, port);
}
catch (FileNotFoundException e) {
try {
FileWriter writer = new FileWriter(FILEPATH);
String simpleJSON = String.format("{\n\t\"%s\": \"%s\",\n\t\"%s\": %d\n}",
HOST, PacketSender.LOCALHOST,
PORT, PacketSender.DEFAULT_PORT
);
writer.write(simpleJSON);
writer.close();
}
catch (Exception e2) {

}
ClientVMC.LOGGER.warn("Config not found, creating default config.");
saveJSON();
return new Pair<String,Integer>(PacketSender.LOCALHOST, PacketSender.DEFAULT_PORT);
}
catch (Exception e) {
ClientVMC.LOGGER.warn("Config could not be read, using default parameters.", e);
return new Pair<String,Integer>(PacketSender.LOCALHOST, PacketSender.DEFAULT_PORT);
}
}

public static void saveJSON () {
try {
FileWriter writer = new FileWriter(FILEPATH);
String simpleJSON = String.format("{\n\t\"%s\": \"%s\",\n\t\"%s\": %d\n}",
HOST, host,
PORT, port
);
writer.write(simpleJSON);
writer.close();
}
catch (Exception e) {

}
}

public static String getIP () {
return host;
}

public static int getPort () {
return port;
}

public static void setIP (String newIP) {
host = newIP;
}

public static void setPort (int newPort) {
if (newPort <= 65535 && newPort > 0) {
port = newPort;
}
else {
ClientVMC.LOGGER.error("Attempted to set illegal port: " + newPort);
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/provismet/vmcmc/config/ModMenuHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.provismet.vmcmc.config;

import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;

import net.fabricmc.loader.api.FabricLoader;

public class ModMenuHook implements ModMenuApi {
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory () {
if (FabricLoader.getInstance().isModLoaded("cloth-config")) {
return parent -> {
return ClothVMC.build(parent);
};
}
else return parent -> null;
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/provismet/vmcmc/vmc/PacketSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
public class PacketSender {
public static final String LOCALHOST = "127.0.0.1";
public static final int DEFAULT_PORT = 35404;
public static final int DEFAULT_PORT = 39540;

private static OSCPortOut portOut;

Expand All @@ -29,6 +29,15 @@ public class PacketSender {
* @param port The port number.
*/
public static void initPort (String host, int port) {
if (portOut != null) {
try {
portOut.close();
}
catch (IOException e) {
ClientVMC.LOGGER.error("Failed to close port:", e);
}
}

try {
portOut = new OSCPortOut(InetAddress.getByName(host), port); // Use 127.0.0.1 and NOT InetAddress.getLocalHost(), the latter does not work.
ClientVMC.LOGGER.info("Created VMC socket at " + host + ":" + port);
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/assets/vmcmc/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"title.vmcmc.config": "Virtual Motion Capture",

"category.vmcmc.general": "General",

"tooltip.vmcmc.general.ip": "Must be a valid IP address. e.g. 127.0.0.1",
"tooltip.vmcmc.general.port": "Must be a value between 1 and 65535.",

"entry.vmcmc.general.ip": "IP Address",
"entry.vmcmc.general.port": "Port"
}
5 changes: 4 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
"entrypoints": {
"client": [
"com.provismet.vmcmc.ClientVMC"
],
"modmenu": [
"com.provismet.vmcmc.config.ModMenuHook"
]
},
"mixins": [
],
"depends": {
"fabricloader": ">=0.14.21",
"minecraft": ">=1.18.2",
"minecraft": ">=1.19",
"java": ">=17",
"fabric-api": "*"
}
Expand Down

0 comments on commit 24a8389

Please sign in to comment.