Skip to content

Commit

Permalink
v3.12.5+fabric
Browse files Browse the repository at this point in the history
- `Shift+LMB` on the `Statistics` button to open the vanilla stats screen. Used as a shortcut.,
- Fixed a consistency bug with closing the BSS screen
- Quick-share actions are now logged for easier debugging purposes
- Quick-share MCBS data is now compressed prior to being uploaded. The compression is not yet applied to downloads however.
  • Loading branch information
TheCSDev committed Jul 30, 2024
1 parent fbe8467 commit 546ddc7
Show file tree
Hide file tree
Showing 28 changed files with 473 additions and 34 deletions.
2 changes: 1 addition & 1 deletion betterstats-3-fabric-1.20.1/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ org.gradle.jvmargs=-Xmx1G
mod_name = Better Statistics Screen
mod_description = Improves the statistics screen and makes it more useful.
mod_author = TheCSDev
mod_version = 3.12.4+fabric-1.20.1
mod_version = 3.12.5+fabric-1.20.1

# Here you link the source code repository links:
mod_contact_homepage = https://github.com/TheCSMods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.github.thecsdev.tcdcommons.api.client.gui.screen.TScreenPlus;
import io.github.thecsdev.tcdcommons.api.client.gui.screen.TScreenWrapper;
import io.github.thecsdev.tcdcommons.api.client.util.interfaces.IParentScreenProvider;
import net.minecraft.client.gui.screen.GameMenuScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.network.packet.c2s.play.ClientStatusC2SPacket;
import net.minecraft.network.packet.c2s.play.ClientStatusC2SPacket.Mode;
Expand Down Expand Up @@ -71,7 +72,14 @@ public BetterStatsScreen(@Nullable Screen parent, IStatsProvider statsProvider)
// ==================================================
public final @Override boolean shouldPause() { return true; }
public final @Override boolean shouldRenderInGameHud() { return false; }
public final @Override void close() { getClient().setScreen(this.parent); }
public final @Override void close()
{
//for non-pause-menu screens, set screen to parent
if(!(this.parent instanceof GameMenuScreen))
getClient().setScreen(this.parent);
//for the pause-menu screen, set screen to null for consistency
else super.close();
}
// --------------------------------------------------
protected final @Override void onOpened()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.github.thecsdev.tcdcommons.api.events.item.ItemGroupEvent;
import io.github.thecsdev.tcdcommons.api.hooks.client.gui.widget.ButtonWidgetHooks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.client.gui.widget.ButtonWidget;

Expand Down Expand Up @@ -50,9 +51,14 @@ public BetterStatsClient()
if(ogStatsBtn == null) return;

//replace its function
final var ogStatsBtn_onPress = ButtonWidgetHooks.getOnPress(ogStatsBtn);
ButtonWidgetHooks.setOnPress(
ogStatsBtn,
btn -> MC_CLIENT.setScreen(new BetterStatsScreen(MC_CLIENT.currentScreen).getAsScreen()));
ogStatsBtn,
btn ->
{
if(Screen.hasShiftDown()) ogStatsBtn_onPress.onPress(ogStatsBtn);
else MC_CLIENT.setScreen(new BetterStatsScreen(MC_CLIENT.currentScreen).getAsScreen());
});
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.thecsdev.betterstats.client.gui.screen;

import static io.github.thecsdev.betterstats.BetterStats.LOGGER;
import static io.github.thecsdev.betterstats.BetterStats.getModID;
import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT;
import static io.github.thecsdev.betterstats.util.io.BetterStatsWebApiUtils.GSON;
Expand Down Expand Up @@ -104,6 +105,11 @@ public QuickShareDownloadScreen(@Nullable Screen bssParent, @Nullable Screen par
this.__error = exception;
if(!isOpen()) return; //break the operation if the user closed the screen
refresh();

//log
LOGGER.error(
"[Quick-Share] Failed to download quick-shared statistics using the code; " + this.quickShareCode,
exception);
}
// --------------------------------------------------
private @Internal void __start__stage1and2and3()
Expand All @@ -115,6 +121,9 @@ public QuickShareDownloadScreen(@Nullable Screen bssParent, @Nullable Screen par
if(!isOpen()) return; //break the operation if the user closed the screen
//note: do not call `refresh()` here

//log
LOGGER.info("[Quick-Share] Downloading quick-shared statistics using the code; " + this.quickShareCode);

//parse the user-input identifier
@Nullable Identifier mcbsCachedId = null;
try { mcbsCachedId = Identifier.of(getModID(), "quick_share/downloads/" + this.quickShareCode); }
Expand Down Expand Up @@ -316,6 +325,9 @@ public QuickShareDownloadScreen(@Nullable Screen bssParent, @Nullable Screen par
final var stats = new RAMStatsProvider(buffer, true);
final var bss = new BetterStatsScreen(this.bssParent, stats);
MC_CLIENT.setScreen(bss.getAsScreen());

//log
LOGGER.info("[Quick-Share] Succesfully downloaded quick-shared statistics using the code; " + this.quickShareCode);
}
catch(Exception exc) { __start_onError(exc); }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.thecsdev.betterstats.client.gui.screen;

import static io.github.thecsdev.betterstats.BetterStats.LOGGER;
import static io.github.thecsdev.betterstats.BetterStats.getModID;
import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT;
import static io.github.thecsdev.betterstats.util.io.BetterStatsWebApiUtils.GSON;
Expand All @@ -12,6 +13,7 @@
import java.time.Instant;
import java.util.Locale;
import java.util.Objects;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -43,7 +45,9 @@
import io.github.thecsdev.tcdcommons.api.util.io.cache.CachedResource;
import io.github.thecsdev.tcdcommons.api.util.io.cache.CachedResourceManager;
import io.github.thecsdev.tcdcommons.api.util.io.cache.IResourceFetchTask;
import io.github.thecsdev.tcdcommons.api.util.math.Tuple2;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.Unpooled;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.network.PacketByteBuf;
Expand Down Expand Up @@ -122,6 +126,12 @@ public QuickShareUploadScreen(@Nullable Screen parent, IStatsProvider stats)
this.__error = exception;
if(!isOpen()) return; //break the operation if the user closed the screen
refresh();

//log
LOGGER.error(
"[Quick-share] Failed to upload quick-share statistics." +
(this.__quickShareCode != null ? " The quick-share code is: " + this.__quickShareCode : ""),
exception);
}
// --------------------------------------------------
private @Internal void __start__stage1()
Expand All @@ -133,6 +143,9 @@ public QuickShareUploadScreen(@Nullable Screen parent, IStatsProvider stats)
if(!isOpen()) return; //break the operation if the user closed the screen
//note: do not call `refresh()` here

//log
LOGGER.info("[Quick-share] Uploading quick-share statistics...");

//fetch the API links
BetterStatsWebApiUtils.fetchBssApiLinksAsync(MC_CLIENT,
json -> __start__stage2(json),
Expand Down Expand Up @@ -271,11 +284,15 @@ public CachedResource<String> fetchResourceSync() throws Exception
httpBody.writeBytes(str.getBytes(Charsets.UTF_8));
}
{
final var statsBytes = exportStatsBytes();
final var str = "--" + multipartBoundary + CRLF +
"Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"" + CRLF +
"Content-Type: application/octet-stream" + CRLF + CRLF;
"Content-Type: application/octet-stream" + CRLF +
"Cache-Control: no-transform" + CRLF +
(statsBytes.Item2 ? "Content-Encoding: gzip" + CRLF : "") +
CRLF;
httpBody.writeBytes(str.getBytes(Charsets.UTF_8));
StatsProviderIO.write(httpBody, QuickShareUploadScreen.this.stats);
httpBody.writeBytes(statsBytes.Item1);
httpBody.writeBytes(CRLF.getBytes(Charsets.UTF_8));
}
httpBody.writeBytes(("--" + multipartBoundary + "--" + CRLF).getBytes(Charsets.UTF_8));
Expand Down Expand Up @@ -337,6 +354,54 @@ public CachedResource<String> fetchResourceSync() throws Exception
this.__stage = 4;
if(!isOpen()) return; //break the operation if the user closed the screen
refresh();

//log
LOGGER.info("[Quick-share] Succesfully uploaded quick-share statistics. The code is: " + this.__quickShareCode);
}
// ==================================================
/**
* Exports the {@link #stats} bytes, in either GZip compressed or
* uncompressed format, whichever takes up less space.
*
* @apiNote May result in duplicate data in-RAM as the compression takes place,
* but the duplicate data is erased from RAM shortly after.
*/
private @Internal Tuple2<byte[], Boolean> exportStatsBytes()
{
//prepare
byte[] raw = null;
byte[] compressed = null;

//obtain the raw statistics
final var rawBuffer = new PacketByteBuf(Unpooled.buffer());
try
{
StatsProviderIO.write(rawBuffer, this.stats);
raw = new byte[rawBuffer.readableBytes()];
rawBuffer.readBytes(raw);
}
finally { rawBuffer.release(); } //Note: Always release to avoid memory leaks.

//obtain the compressed statistics
final var compressedBuffer = new PacketByteBuf(Unpooled.buffer());
try
{
final var outputStream = new ByteBufOutputStream(compressedBuffer);
try (var gzipOutputStream = new GZIPOutputStream(outputStream)) { gzipOutputStream.write(raw); }
catch (IOException e) { throw new RuntimeException("Error during GZip compression of MCBS data", e); }

compressed = new byte[compressedBuffer.readableBytes()];
compressedBuffer.readBytes(compressed);
}
finally { compressedBuffer.release(); } //Note: Always release to avoid memory leaks.

//log
LOGGER.info("[Quick-share] Attempting to compress quick-share MCBS data using GZip. Raw file size is " +
raw.length + ", and compressed file size is " + compressed.length + ".");

//return the shortest outcome
if(compressed.length < raw.length) return new Tuple2<>(compressed, true);
else return new Tuple2<>(raw, false);
}
// ==================================================
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"minecraft": "${project.properties.mod_depends_minecraft}",
"java": ">=17",
"fabric-api": "*",
"tcdcommons": ">=3.12"
"tcdcommons": ">=3.12.2"
},

"custom":
Expand Down
2 changes: 1 addition & 1 deletion betterstats-3-fabric-1.20.2/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ org.gradle.jvmargs=-Xmx1G
mod_name = Better Statistics Screen
mod_description = Improves the statistics screen and makes it more useful.
mod_author = TheCSDev
mod_version = 3.12.4+fabric-1.20.2
mod_version = 3.12.5+fabric-1.20.2

# Here you link the source code repository links:
mod_contact_homepage = https://github.com/TheCSMods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.github.thecsdev.tcdcommons.api.client.gui.screen.TScreenPlus;
import io.github.thecsdev.tcdcommons.api.client.gui.screen.TScreenWrapper;
import io.github.thecsdev.tcdcommons.api.client.util.interfaces.IParentScreenProvider;
import net.minecraft.client.gui.screen.GameMenuScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.network.packet.c2s.play.ClientStatusC2SPacket;
import net.minecraft.network.packet.c2s.play.ClientStatusC2SPacket.Mode;
Expand Down Expand Up @@ -71,7 +72,14 @@ public BetterStatsScreen(@Nullable Screen parent, IStatsProvider statsProvider)
// ==================================================
public final @Override boolean shouldPause() { return true; }
public final @Override boolean shouldRenderInGameHud() { return false; }
public final @Override void close() { getClient().setScreen(this.parent); }
public final @Override void close()
{
//for non-pause-menu screens, set screen to parent
if(!(this.parent instanceof GameMenuScreen))
getClient().setScreen(this.parent);
//for the pause-menu screen, set screen to null for consistency
else super.close();
}
// --------------------------------------------------
protected final @Override void onOpened()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.github.thecsdev.tcdcommons.api.events.item.ItemGroupEvent;
import io.github.thecsdev.tcdcommons.api.hooks.client.gui.widget.ButtonWidgetHooks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.client.gui.widget.ButtonWidget;

Expand Down Expand Up @@ -50,9 +51,14 @@ public BetterStatsClient()
if(ogStatsBtn == null) return;

//replace its function
final var ogStatsBtn_onPress = ButtonWidgetHooks.getOnPress(ogStatsBtn);
ButtonWidgetHooks.setOnPress(
ogStatsBtn,
btn -> MC_CLIENT.setScreen(new BetterStatsScreen(MC_CLIENT.currentScreen).getAsScreen()));
ogStatsBtn,
btn ->
{
if(Screen.hasShiftDown()) ogStatsBtn_onPress.onPress(ogStatsBtn);
else MC_CLIENT.setScreen(new BetterStatsScreen(MC_CLIENT.currentScreen).getAsScreen());
});
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.thecsdev.betterstats.client.gui.screen;

import static io.github.thecsdev.betterstats.BetterStats.LOGGER;
import static io.github.thecsdev.betterstats.BetterStats.getModID;
import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT;
import static io.github.thecsdev.betterstats.util.io.BetterStatsWebApiUtils.GSON;
Expand Down Expand Up @@ -104,6 +105,11 @@ public QuickShareDownloadScreen(@Nullable Screen bssParent, @Nullable Screen par
this.__error = exception;
if(!isOpen()) return; //break the operation if the user closed the screen
refresh();

//log
LOGGER.error(
"[Quick-Share] Failed to download quick-shared statistics using the code; " + this.quickShareCode,
exception);
}
// --------------------------------------------------
private @Internal void __start__stage1and2and3()
Expand All @@ -115,6 +121,9 @@ public QuickShareDownloadScreen(@Nullable Screen bssParent, @Nullable Screen par
if(!isOpen()) return; //break the operation if the user closed the screen
//note: do not call `refresh()` here

//log
LOGGER.info("[Quick-Share] Downloading quick-shared statistics using the code; " + this.quickShareCode);

//parse the user-input identifier
@Nullable Identifier mcbsCachedId = null;
try { mcbsCachedId = Identifier.of(getModID(), "quick_share/downloads/" + this.quickShareCode); }
Expand Down Expand Up @@ -316,6 +325,9 @@ public QuickShareDownloadScreen(@Nullable Screen bssParent, @Nullable Screen par
final var stats = new RAMStatsProvider(buffer, true);
final var bss = new BetterStatsScreen(this.bssParent, stats);
MC_CLIENT.setScreen(bss.getAsScreen());

//log
LOGGER.info("[Quick-Share] Succesfully downloaded quick-shared statistics using the code; " + this.quickShareCode);
}
catch(Exception exc) { __start_onError(exc); }
}
Expand Down
Loading

0 comments on commit 546ddc7

Please sign in to comment.