Skip to content

Commit

Permalink
port to 1.21-rc1
Browse files Browse the repository at this point in the history
- Port to 1.21-rc1
- Refactor crash report handling
  • Loading branch information
maximumpower55 committed Jun 11, 2024
1 parent 802cbb5 commit 169a1eb
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 55 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ processResources {

filesMatching("fabric.mod.json") {
expand "version": project.version,
"mcversion": project.minecraft_version
"mcversion": "1.21-beta.3"
}
}

Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ org.gradle.jvmargs=-Xmx4G

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.20.6
loader_version=0.15.10
fabric_version=0.97.8+1.20.6
minecraft_version=1.21-rc1
loader_version=0.15.11
fabric_version=0.100.0+1.21

# Mod Properties
mod_version = 1.2.3
mod_version = 1.3.0
maven_group = one.devos.nautical
archives_base_name = teabridge
2 changes: 1 addition & 1 deletion src/main/java/one/devos/nautical/teabridge/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public record Config(

public static final Config DEFAULT = new Config(Discord.DEFAULT, Avatars.DEFAULT, Game.DEFAULT, Crashes.DEFAULT, false);

public static DataResult<Config> load(Path path) {
public static DataResult<Config> loadOrCreate(Path path) {
try {
if (Files.exists(path)) {
try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/one/devos/nautical/teabridge/TeaBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,29 @@ public class TeaBridge implements DedicatedServerModInitializer {

public static Config config = Config.DEFAULT;

@Override
public void onInitializeServer() {
ServerLifecycleEvents.SERVER_STARTING.register(this::onServerStarting);
ServerLifecycleEvents.SERVER_STARTED.register(this::onServerStart);
ServerLifecycleEvents.SERVER_STOPPED.register(this::onServerStop);

ResourceLocation phaseId = new ResourceLocation(MOD_ID, "mirror");
ServerMessageEvents.CHAT_MESSAGE.addPhaseOrdering(new ResourceLocation("switchy_proxy", "set_args"), phaseId);
ServerMessageEvents.CHAT_MESSAGE.addPhaseOrdering(phaseId, new ResourceLocation("switchy_proxy", "clear"));
ResourceLocation phaseId = ResourceLocation.fromNamespaceAndPath(MOD_ID, "mirror");
ServerMessageEvents.CHAT_MESSAGE.addPhaseOrdering(ResourceLocation.fromNamespaceAndPath("switchy_proxy", "set_args"), phaseId);
ServerMessageEvents.CHAT_MESSAGE.addPhaseOrdering(phaseId, ResourceLocation.fromNamespaceAndPath("switchy_proxy", "clear"));
ServerMessageEvents.CHAT_MESSAGE.register(phaseId, this::onChatMessage);

ServerMessageEvents.COMMAND_MESSAGE.register(this::onCommandMessage);

CommandRegistrationCallback.EVENT.register(this::registerCommands);

Config.load(CONFIG_PATH)
Config.loadOrCreate(CONFIG_PATH)
.ifError(e -> LOGGER.error("Failed to load config using defaults : {}", e))
.ifSuccess(loaded -> config = loaded);
Discord.start();
}

private void onServerStarting(MinecraftServer server) {
if (TeaBridge.config.debug()) TeaBridge.LOGGER.warn("DEBUG MODE IS ENABLED, THIS WILL LOG EVERYTHING WILL CAUSE LAG SPIKES!!!!!!");
if (TeaBridge.config.debug()) TeaBridge.LOGGER.warn("!!Debug mode enabled in config!!");
Discord.send(TeaBridge.config.game().serverStartingMessage());
}

Expand All @@ -72,7 +73,7 @@ private void onServerStart(MinecraftServer server) {
}

private void onServerStop(MinecraftServer server) {
if (!CrashHandler.CRASH_VALUE.get()) Discord.send(TeaBridge.config.game().serverStopMessage());
if (!CrashHandler.didCrash) Discord.send(TeaBridge.config.game().serverStopMessage());
Discord.stop();
}

Expand All @@ -96,7 +97,7 @@ private void registerCommands(CommandDispatcher<CommandSourceStack> dispatcher,
Commands.literal("reloadConfig")
.executes(command -> {
CommandSourceStack source = command.getSource();
DataResult<Config> loadResult = Config.load(CONFIG_PATH);
DataResult<Config> loadResult = Config.loadOrCreate(CONFIG_PATH);
loadResult
.ifError(e -> {
source.sendFailure(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package one.devos.nautical.teabridge.mixin;

import java.io.File;
import java.nio.file.Path;
import java.util.List;

import one.devos.nautical.teabridge.TeaBridge;
import net.minecraft.ReportType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -13,10 +14,8 @@

@Mixin(CrashReport.class)
public abstract class CrashReportMixin {
@Inject(method = "saveToFile", at = @At("HEAD"))
private void uploadCrash(File file, CallbackInfoReturnable<Boolean> cir) {
CrashHandler.CRASH_VALUE.crash(() -> {
if (TeaBridge.config.crashes().uploadToMclogs()) CrashHandler.uploadAndSend((CrashReport) (Object) this);
});
@Inject(method = "saveToFile(Ljava/nio/file/Path;Lnet/minecraft/ReportType;Ljava/util/List;)Z", at = @At("HEAD"))
private void handleCrash(Path path, ReportType reportType, List<String> list, CallbackInfoReturnable<Boolean> cir) {
CrashHandler.handle((CrashReport) (Object) this);
}
}
53 changes: 18 additions & 35 deletions src/main/java/one/devos/nautical/teabridge/util/CrashHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,29 @@
import one.devos.nautical.teabridge.discord.Discord;

public class CrashHandler {
public static final CrashValue CRASH_VALUE = new CrashValue() {
private boolean value = false;

@Override
public boolean get() {
return value;
}

@Override
public void crash(Runnable onCrash) {
if (!value) {
value = true;
Discord.send(TeaBridge.config.game().serverCrashMessage());
onCrash.run();
}
}
};
public static boolean didCrash = false;

private static final URI LOG_UPLOAD_URI = URI.create("https://api.mclo.gs/1/log");

private static final Codec<String> LOG_UPLOAD_RESPONSE_CODEC = Codec.STRING.fieldOf("url").codec();

public static void uploadAndSend(final CrashReport crash) {
String message;
try {
HttpResponse<String> response = TeaBridge.CLIENT.send(HttpRequest.newBuilder(LOG_UPLOAD_URI)
.POST(HttpRequest.BodyPublishers.ofString("content=" + URLEncoder.encode(crash.getDetails(), StandardCharsets.UTF_8)))
.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.build(), HttpResponse.BodyHandlers.ofString());
if (response.statusCode() / 100 != 2) throw new Exception("Non-success status code from request " + response);
message = LOG_UPLOAD_RESPONSE_CODEC.parse(JsonOps.INSTANCE, JsonParser.parseString(response.body())).getOrThrow();
} catch (Exception e) {
message = "Failed to upload crash report : " + e.getMessage();
}

Discord.send(message);
}
public static void handle(CrashReport crash) {
didCrash = true;
if (TeaBridge.config.crashes().uploadToMclogs()) {
String message;
try {
HttpResponse<String> response = TeaBridge.CLIENT.send(HttpRequest.newBuilder(LOG_UPLOAD_URI)
.POST(HttpRequest.BodyPublishers.ofString("content=" + URLEncoder.encode(crash.getDetails(), StandardCharsets.UTF_8)))
.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.build(), HttpResponse.BodyHandlers.ofString());
if (response.statusCode() / 100 != 2)
throw new Exception("Non-success status code from request " + response);
message = LOG_UPLOAD_RESPONSE_CODEC.parse(JsonOps.INSTANCE, JsonParser.parseString(response.body())).getOrThrow();
} catch (Exception e) {
message = "Failed to upload crash report : " + e.getMessage();
}

public interface CrashValue {
boolean get();
void crash(Runnable onCrash);
Discord.send(message);
}
}
}

0 comments on commit 169a1eb

Please sign in to comment.