Skip to content

Commit

Permalink
Stable Beta now - 0.9.3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
wanomaniac committed Aug 19, 2024
1 parent 7b17191 commit 76e8ede
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 50 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ yarn_mappings=1.20.1+build.10
loader_version=0.16.0

# Mod Properties
mod_version=0.9.2.7
config_version=0.9.5.2-JavaScript Integration
mod_version=0.9.3.5
config_version=0.9.5.5-c8711284
maven_group=com.skellybuilds.SCMC
archives_base_name=scmc

Expand Down
109 changes: 76 additions & 33 deletions src/main/java/com/skellybuilds/SCMC/SCMC.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.skellybuilds.SCMC;

import com.mojang.authlib.exceptions.MinecraftClientException;

import com.skellybuilds.SCMC.config.ModMenuConfig;
import com.skellybuilds.SCMC.events.ServerJoinProcessing;
import com.skellybuilds.SCMC.events.ServerLoginProcessing;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.networking.v1.ServerLoginConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
Expand All @@ -16,30 +18,57 @@

import net.minecraft.server.MinecraftServer;
import net.minecraft.text.Text;
import net.minecraft.util.crash.CrashException;
import net.minecraft.util.crash.CrashReport;
import net.minecraft.util.crash.CrashReportSection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

@Environment(EnvType.SERVER)
public class SCMC implements ModInitializer {
public static final String MOD_ID = "SCMC";
public static final Logger LOGGER = LoggerFactory.getLogger("SCMC [Server Client Mods Checker]");
public static Network svr = new Network(25572);
public static Network svr = new Network(27752);
public Network.ServerCommands cmds = new Network.ServerCommands();
public static final Map<String, String[]> PlayerN = new HashMap<>();
public static final ModContainer MainModContainer = FabricLoader.getInstance().getModContainer("scmc").orElse(null);
private boolean registerd = false;

public void setCrash(Exception error, MinecraftServer server){
LocalDateTime now = LocalDateTime.now();

server.sendMessage(Text.literal("A crash occured from SCMC! Show this to any admins"));

CrashReport crashReport = new CrashReport("SCMC has well crashed :( *add dramatic car crash sounds* \n\n", new RuntimeException(error.getMessage()));

CrashReportSection section = crashReport.addElement("Main suspect");
section.add("Stack trace", error.fillInStackTrace());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss.SSS");

File dafile = new File("./crash-reports/SCMC-CRASH-"+ now.format(formatter) +".txt");

private boolean isCrashed = false;
private CrashReport crashRP;
LOGGER.error(crashReport.asString());
crashReport.writeToFile(dafile);
String fullpath = "UNABLE TO GET PATH! SEARCH IN CRASH REPORTS FOLDER";
try {
fullpath = dafile.getCanonicalPath();
} catch (IOException e) {
LOGGER.error(e.toString());
}
LOGGER.info("Crash report has been saved at .{}! \n If you can't manage to solve it, please report it at https://github.com/SkellyBuilds/scmc/issues", fullpath);
LOGGER.error("Shutting down the server! Read the crash report for more information!");
server.stop(true);
}

@Override
public void onInitialize() {


ModMenuConfigManager.prepareConfigFile();
if (!ModMenuConfigManager.fileExistant()) {
LOGGER.info(Text.translatable("scmc.console.welcome", Text.translatable("scmc.welcome.header"), MainModContainer.getMetadata().getVersion()).getString());
Expand All @@ -49,8 +78,12 @@ public void onInitialize() {
LOGGER.info(Text.translatable("scmc.console.initalize", Text.translatable("scmc.header"), MainModContainer.getMetadata().getVersion()).getString());
}

Thread main = new Thread(() -> {
try {
svr.setPort(ModMenuConfig.CPORT.getValue());

ServerLifecycleEvents.SERVER_STARTED.register((server) -> {
if(!registerd) {
registerd = true;
Thread main = new Thread(() -> {
Thread connection = new Thread(() -> {
try {
LOGGER.info("Initalizing - Connection");
Expand All @@ -60,29 +93,48 @@ public void onInitialize() {
cmds.addCommand("getmod", servercmd::getModName);
cmds.addCommand("addpmods", servercmd::verifyPlayerMods);
svr.init(cmds);
LOGGER.info("Connection - Ready");
while (true) {
if (!svr.isOnline && svr.crashed) {
LOGGER.error("NETWORkING HAS CRASHED!");
Exception test = new Exception("Critical exception while running connection thread - " + svr.howC + "\n \nIf this crash is related to your port, fix your permissions or configuration to a proper setting!");
setCrash(test, server);
break;
} else if (svr.isOnline && !svr.crashed) {
break;
}
}

if(svr.isOnline)LOGGER.info("Connection - Ready");
} catch (Exception error) {
LOGGER.error("Exception occured while initalizing connection thread \n {}", error.getMessage());
try {
throw new Exception("Critical exception while running connection thread");
throw new Exception();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});

connection.setUncaughtExceptionHandler((thread, error) -> {
setCrash((Exception) error, server);
});

connection.setName("Main Thread - Connection - SCMC");
connection.setPriority(1);


Thread events = new Thread(() -> {
LOGGER.info("Initalizing - Events");
if(!ModMenuConfig.CANKICKPLAYERSWITHNOMODS.getValue()){
if (!ModMenuConfig.CANKICKPLAYERSWITHNOMODS.getValue()) {
ServerPlayConnectionEvents.JOIN.register(ServerJoinProcessing::onEventRegister);
} else ServerLoginConnectionEvents.QUERY_START.register(ServerLoginProcessing::onEventRegister);
LOGGER.info("Events - Ready");
});

events.setUncaughtExceptionHandler((thread, error) -> {
setCrash((Exception) error, server);
});

events.setName("Main Thread - Events - SCMC");
connection.setPriority(2);

Expand All @@ -92,33 +144,24 @@ public void onInitialize() {
boolean isCOMP = false;
do {
if (connection.getState() != Thread.State.RUNNABLE && events.getState() != Thread.State.RUNNABLE) {
LOGGER.info("Everything is OK!");
LOGGER.info("All sub threads have been completed");
isCOMP = true;
}
} while (!isCOMP);

while (true) {
if (svr.crashed) {
throw new Exception("Networking has crashed! This is very fatal - " + svr.howC);
}
}
} catch (Exception error) {
LOGGER.error("Exception occured while running the main thread \n {}", error.getMessage());
CrashReport crashReport = new CrashReport("Welp, something went wrong in SCMC :(", new RuntimeException("SCMC has CRAAAASHED *add dramatic car crash sounds*"));
});

CrashReportSection section = crashReport.addElement("Main suspect");
section.add("Error message", error.getMessage());
section.add("Cause", error.getCause());
section.add("Stack trace", error.getStackTrace());
crashReport.getSystemDetailsSection();
main.setName("Main Thread - index - SCMC");

crashRP = crashReport;
throw new CrashException(crashRP);
}
});

main.setName("Main Thread - index - SCMC");
main.start();
main.start();

}

}
});




}
}
17 changes: 9 additions & 8 deletions src/main/java/com/skellybuilds/SCMC/config/ModMenuConfig.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.skellybuilds.SCMC.config;

import com.skellybuilds.SCMC.config.option.*;
import net.minecraft.text.Text;

import java.util.HashSet;


public class ModMenuConfig {
// Move default comments to langs?
//public static final BooleanConfigOption ALLOWSERVERMODMENUCONNECTION = new BooleanConfigOption("allowservermodmenudownloads", true);
public static final BooleanConfigOption USEONLYCLIENTMODS = new BooleanConfigOption("useonlyuniversalmods", true, "Universal mods basically are both client side & server side mods. So one for both! \n If you leave this enabled, server mods will not be used unless you specified otherwise");
public static final BooleanConfigOption CANKICKPLAYERSWITHNOMODS = new BooleanConfigOption("cankickplayerswithnomods", true, "This gives you the choice to allow the mod to kick players if they are missing a mod or otherwise \n If you choose false, it won't kick the player but notify the player about the missing mods so they can be aware!");
public static final StringSetConfigOption USETHISMODSONLY = new StringSetConfigOption("usethismodsonly", new HashSet<>(), "This is an array that will only use the mod ids provided for the mod, nothing else. \n If you leave it empty, All mods will be used by SCMC unless you configurated it otherwise");
public static final StringSetConfigOption DONTUSETHISMODS = new StringSetConfigOption("dontusethismods", new HashSet<>(), "This will basically blacklist a mod from being registered in SCMC, if you have it usethismods, the blacklist won't be checked since the whitelist is always checked first. ");
public static final StringSetConfigOption USETHISMODS = new StringSetConfigOption("usethismods", new HashSet<>(), "This is basically an exception, which ignores everything and registers anyways, sort of like a whitelist");
public static final StringSetConfigOption OPTIONALMODS = new StringSetConfigOption("optionalmods", new HashSet<>(), "This list of mods won't be required to download, meaning they won't show up in the missing mod screen and there will be a GUI difference for the mod to clarify via servermodmenu");
public static final BooleanConfigOption USEONLYCLIENTMODS = new BooleanConfigOption("useonlyuniversalmods", true, Text.translatable("scmc.config.desc.useonlyuniversalmods").getString());
public static final BooleanConfigOption CANKICKPLAYERSWITHNOMODS = new BooleanConfigOption("cankickplayerswithnomods", true, Text.translatable("scmc.config.desc.cankickplayerswithnomods").getString());
public static final StringSetConfigOption USETHISMODSONLY = new StringSetConfigOption("usethismodsonly", new HashSet<>(), Text.translatable("scmc.config.desc.usethismodsonly").getString());
public static final StringSetConfigOption DONTUSETHISMODS = new StringSetConfigOption("dontusethismods", new HashSet<>(), Text.translatable("scmc.config.desc.dontusethismods").getString());
public static final StringSetConfigOption USETHISMODS = new StringSetConfigOption("usethismods", new HashSet<>(), Text.translatable("scmc.config.desc.useonlyuniversalmods").getString());
public static final StringSetConfigOption OPTIONALMODS = new StringSetConfigOption("optionalmods", new HashSet<>(), Text.translatable("scmc.config.desc.optionalmods").getString());
public static final IntConfigOption CPORT = new IntConfigOption("port", 27752, Text.translatable("scmc.config.desc.port").getString());
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ private static void load() {
}
if (file.exists()) {
Gson gson = new Gson();
JsonObject json;
JsonObject json = null;
StringBuilder cJson = new StringBuilder();
Map<String, String> cJK = new HashMap<>();
String cVersion = "";
boolean canUpdateConfig = true;

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
Expand All @@ -92,8 +94,19 @@ private static void load() {
String mtlC = "";
boolean startStuff = false;


while ((line = br.readLine()) != null ) {

if(line.contains("DISABLE-CONFIG-UPDATES")){
canUpdateConfig = false;
}

if(line.contains("Config Version:")){
line = line.substring(0, line.length()-2);
int lD = line.indexOf(": ");
cVersion = line.substring(lD+2);
}

int comment1LIndex = line.indexOf("//");

if(!startStuff) {
Expand All @@ -109,6 +122,7 @@ private static void load() {
int beforeKS = line.indexOf(":");
if (beforeKS == -1){
line = line.substring(0, comment1LIndex);
cJson.append(line);
continue;
}; // No key found

Expand Down Expand Up @@ -165,8 +179,18 @@ private static void load() {
}
}

json = gson.fromJson(cJson.toString(), JsonObject.class);
if(cJson.isEmpty()){
save();
load();
return;
}

try {
json = gson.fromJson(cJson.toString(), JsonObject.class);
} catch (JsonSyntaxException e) {
SCMC.LOGGER.error("Failed to load Config: Parsed JS to JSON file has failed \n More Details: \n {}\n\n^\n| - Error information related to config \n Don't understand, need help or you believe this is a bug?\n Report to https://github.com/SkellyBuilds/scmc/issues", e.toString());
throw new RuntimeException("Config parsing failed! Scroll to the error");
}
for (Field field : ModMenuConfig.class.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) {
if (StringSetConfigOption.class.isAssignableFrom(field.getType())) {
Expand All @@ -185,9 +209,23 @@ private static void load() {
ConfigOptionStorage.setBoolean(option.getKey(), jsonPrimitive.getAsBoolean());
if(cJK.get(option.getKey()) != null) ConfigOptionStorage.setComment(option.getKey(), cJK.get(option.getKey()));
}
} else if(IntConfigOption.class.isAssignableFrom(field.getType())){
IntConfigOption option = (IntConfigOption) field.get(null);
JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive(option.getKey());
if (jsonPrimitive != null && jsonPrimitive.isNumber()) {
ConfigOptionStorage.setInt(option.getKey(), jsonPrimitive.getAsInt());
if(cJK.get(option.getKey()) != null) ConfigOptionStorage.setComment(option.getKey(), cJK.get(option.getKey()));
}
}
}
}

if(!cVersion.isEmpty() && canUpdateConfig){
if(!cVersion.equals(SCMC.MainModContainer.getMetadata().getCustomValue("configV").getAsString())){
save(); // changes the config version to latest header & version so basically an auto update
}
}

}
} catch (IllegalAccessException | IOException e) {
System.err.println("Couldn't load Mod Menu configuration file; reverting to defaults");
Expand Down Expand Up @@ -220,6 +258,12 @@ public static void save() {
if(!option.getComment().isEmpty()){
cJK.put(option.getKey(), option.getComment());
}
} else if(IntConfigOption.class.isAssignableFrom(field.getType())){
IntConfigOption option = (IntConfigOption) field.get(null);
config.addProperty(option.getKey(), ConfigOptionStorage.getInt(option.getKey()));
if(!option.getComment().isEmpty()){
cJK.put(option.getKey(), option.getComment());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ public class ConfigOptionStorage {
private static final Map<String, Boolean> BOOLEAN_OPTIONS = new HashMap<>();
private static final Map<String, Set<String>> STRING_SET_OPTIONS = new HashMap<>();
private static final Map<String, String> COMMENT_DATA = new HashMap<>(); // Key, Comment
private static final Map<String, Integer> INT_OPTIONS = new HashMap<>();

public static void setInt(String key, Integer number){
INT_OPTIONS.put(key, number);
}

public static Integer getInt(String key){
return INT_OPTIONS.get(key);
}

public static void setStringSet(String key, Set<String> value) {
STRING_SET_OPTIONS.put(key, value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.skellybuilds.SCMC.config.option;


public class IntConfigOption {
private final String key;
private final Integer defaultValue;
private final String defaultComment;

public IntConfigOption(String key, Integer defaultValue, String defaultComment) {
ConfigOptionStorage.setInt(key, defaultValue);
this.key = key;
this.defaultValue = defaultValue;
this.defaultComment = defaultComment;
ConfigOptionStorage.setComment(key, defaultComment);
}

public IntConfigOption(String key, Integer defaultValue){this(key, defaultValue, "");}

public String getKey() {
return key;
}

public Integer getValue() {
return ConfigOptionStorage.getInt(key);
}

public String getComment() {return ConfigOptionStorage.getComment(key); } // I don't know when i'll will use this but maybe some day it will be useful for experimenting

public void setComment(String comment){ConfigOptionStorage.setComment(key, comment);}

public void setValue(Integer value) {
ConfigOptionStorage.setInt(key, value);
}


public Integer getDefaultValue() {
return defaultValue;
}

public String getDefaultComment(){ return defaultComment; }





}
Loading

0 comments on commit 76e8ede

Please sign in to comment.