Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id "architectury-plugin" version "3.4-SNAPSHOT"
id "dev.architectury.loom" version "1.11-SNAPSHOT" apply false
id "dev.architectury.loom" version "1.13-SNAPSHOT" apply false
}

architectury {
Expand All @@ -23,14 +23,21 @@ subprojects {
maven { url "https://maven.isxander.dev/releases" }
maven { url "https://maven.terraformersmc.com/" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
// TODO: remove on release
maven {
name = "Maven for PR #2815" // https://github.com/neoforged/NeoForge/pull/2815
url = uri("https://prmaven.neoforged.net/NeoForge/pr2815")
content {
includeModule("net.neoforged", "neoforge")
includeModule("net.neoforged", "testframework")
}
}
}

dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings loom.layered() {
officialMojangMappings()
// TODO: reenable
//parchment("org.parchmentmc.data:parchment-${project.minecraft_version}:${project.parchment_version}@zip")
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ dependencies {

compileOnly("org.apache.commons:commons-math3:${rootProject.math3_version}")
compileOnly "com.github.umjammer:jlayer:${rootProject.jlayer_version}"
compileOnly("org.java-websocket:Java-WebSocket:${rootProject.java_websocket_version}") {
exclude module: "slf4j-api"
}
}

loom {
Expand Down
19 changes: 6 additions & 13 deletions common/src/main/java/io/github/kabanfriends/craftgr/CraftGR.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
import io.github.kabanfriends.craftgr.overlay.SongInfoOverlay;
import io.github.kabanfriends.craftgr.song.FallbackSongProvider;
import io.github.kabanfriends.craftgr.song.SongProvider;
import io.github.kabanfriends.craftgr.util.Http;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.FontDescription;
import net.minecraft.network.chat.Style;
import net.minecraft.resources.ResourceLocation;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import net.minecraft.resources.Identifier;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -26,16 +25,15 @@ public class CraftGR {
public static final String MOD_ID = "craftgr";
public static final String MOD_NAME = "CraftGR";

public static final Component AUDIO_MUTED_ICON = Component.literal("M").withStyle(Style.EMPTY.withFont(new FontDescription.Resource(ResourceLocation.fromNamespaceAndPath(CraftGR.MOD_ID, "icons"))));
public static final Component RECONNECT_ICON = Component.literal("R").withStyle(Style.EMPTY.withFont(new FontDescription.Resource(ResourceLocation.fromNamespaceAndPath(CraftGR.MOD_ID, "icons"))));
public static final Component AUDIO_MUTED_ICON = Component.literal("M").withStyle(Style.EMPTY.withFont(new FontDescription.Resource(Identifier.fromNamespaceAndPath(CraftGR.MOD_ID, "icons"))));
public static final Component RECONNECT_ICON = Component.literal("R").withStyle(Style.EMPTY.withFont(new FontDescription.Resource(Identifier.fromNamespaceAndPath(CraftGR.MOD_ID, "icons"))));

private static CraftGR instance;

private final PlatformAdapter platformAdapter;
private final Logger logger;
private final ModConfig config;
private final ExecutorService executor;
private final CloseableHttpClient httpClient;
private final ClientEvents events;
private final Keybinds keybinds;
private final SongInfoOverlay songInfoOverlay;
Expand All @@ -50,23 +48,18 @@ public CraftGR(PlatformAdapter platformAdapter) {
this.logger = LogManager.getLogger();
this.config = new ModConfig(this);
this.executor = Executors.newCachedThreadPool();
this.httpClient = HttpClients.custom()
.setUserAgent("Minecraft-CraftGR/" + platformAdapter.getModVersion())
.build();
this.events = new ClientEvents(this);
this.keybinds = new Keybinds(this);
this.songInfoOverlay = new SongInfoOverlay(this);
this.radio = new Radio(this);

Http.createClient();
}

public PlatformAdapter getPlatformAdapter() {
return platformAdapter;
}

public CloseableHttpClient getHttpClient() {
return httpClient;
}

public ExecutorService getThreadExecutor() {
return executor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.github.kabanfriends.craftgr.util.ExceptionUtil;
import io.github.kabanfriends.craftgr.util.RingBuffer;
import javazoom.jl.decoder.*;
import net.minecraft.Util;
import net.minecraft.util.Util;
import org.apache.logging.log4j.Level;
import org.lwjgl.BufferUtils;
import org.lwjgl.openal.AL10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import io.github.kabanfriends.craftgr.CraftGR;
import io.github.kabanfriends.craftgr.config.ModConfig;
import io.github.kabanfriends.craftgr.util.*;
import io.github.kabanfriends.craftgr.util.Http;
import javazoom.jl.decoder.JavaLayerException;
import org.apache.http.client.methods.*;
import org.apache.logging.log4j.Level;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpResponse;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -23,7 +24,6 @@ public class Radio {

private State state;
private AudioPlayer audioPlayer;
private CloseableHttpResponse response;
private Future<?> playback;
private boolean hasError;

Expand All @@ -44,7 +44,6 @@ public void stop(boolean awaitReload) {
audioPlayer.stop();
}
if (state == State.CONNECTING || state == State.PLAYING) {
disconnect();
playback.cancel(true);
}

Expand Down Expand Up @@ -114,23 +113,13 @@ private void handlePlayback(boolean fadeIn) {
}

private void connect() throws IOException {
if (response != null) response.close();

HttpGet get = HttpUtil.get(ModConfig.get("urlStream"));
CloseableHttpResponse response = craftGR.getHttpClient().execute(get);
this.response = response;

InputStream stream = response.getEntity().getContent();

audioPlayer = new AudioPlayer(craftGR, stream);
}

private void disconnect() {
try {
response.close();
} catch (IOException e) {
craftGR.log(Level.ERROR, "Error while closing the stream response: " + ExceptionUtil.getStackTrace(e));
}
Http.fetch(Http.standardRequest()
.uri(URI.create(ModConfig.get("urlStream")))
.build(), HttpResponse.BodyHandlers.ofInputStream())
.thenAccept(response -> {
this.audioPlayer = new AudioPlayer(craftGR, response.body());
})
.join();
}

public State getState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.github.kabanfriends.craftgr.overlay.SongInfoOverlay;
import io.github.kabanfriends.craftgr.song.SongProviderType;
import io.github.kabanfriends.craftgr.util.ExceptionUtil;
import io.github.kabanfriends.craftgr.util.Http;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import org.apache.logging.log4j.Level;
Expand All @@ -37,9 +38,9 @@ public class ModConfig {
new ConfigGroup(Component.translatable("text.craftgr.config.category.playback"), true,
new RadioStateConfigField("playback"),
new IntegerConfigField("volume", 50)
.setFormatter((value) -> Component.literal(value + "%"))
.setFormatter(value -> Component.literal(value + "%"))
.setRange(0, 100)
.onApply((value) -> {
.onApply(value -> {
CraftGR.getInstance().getRadio().setVolume(value);
})
),
Expand All @@ -50,9 +51,9 @@ public class ModConfig {
new BooleanConfigField("hideAlbumArt", false),
new BooleanConfigField("openAlbum", true),
new IntegerConfigField("overlayWidth", 115)
.setFormatter((value) -> Component.literal(value + "px"))
.setFormatter(value -> Component.literal(value + "px"))
.setRange(35, 435)
.onApply((value) -> {
.onApply(value -> {
SongInfoOverlay overlay = CraftGR.getInstance().getSongInfoOverlay();
if (overlay != null) {
overlay.updateScrollWidth();
Expand All @@ -64,17 +65,18 @@ public class ModConfig {
),
new ConfigGroup(Component.translatable("text.craftgr.config.category.advanced"), true,
new EnumConfigField("songProvider", SongProviderType.JSON_API)
.onApply((value) -> {
.onApply(value -> {
CraftGR.getInstance().setSongProvider(((SongProviderType) value).createProvider());
}),
new StringConfigField("urlStream", "https://stream.gensokyoradio.net/1/"),
new StringConfigField("urlInfoJson", "https://gensokyoradio.net/api/station/playing/"),
new StringConfigField("urlAlbumArt", "https://gensokyoradio.net/images/albums/500/"),
new StringConfigField("urlWebSocket", "wss://gensokyoradio.net/wss"),
new IntegerConfigField("connectTimeout", 20_000)
.setFormatter((value) -> Component.literal(value + "ms")),
.setFormatter(value -> Component.literal(value + "ms"))
.onApply(value -> Http.createClient()),
new IntegerConfigField("socketTimeout", 10_000)
.setFormatter((value) -> Component.literal(value + "ms"))
.setFormatter(value -> Component.literal(value + "ms"))
)
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ public void onClientStart() {

public void onClientStop() {
craftGR.getRadio().stop(false);

try {
craftGR.getSongProvider().stop();
craftGR.getHttpClient().close();
} catch (IOException ignored) {
}
craftGR.getSongProvider().stop();
}

public void onClientTick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.resources.Identifier;
import org.jetbrains.annotations.Nullable;

import java.util.List;
Expand All @@ -24,9 +24,9 @@ public class RadioOptionContainer extends AbstractContainerWidget {
private static final Component DISABLED_TOOLTIP = Component.translatable("text.craftgr.button.config.disabled");

private static final WidgetSprites CONFIG_BUTTON_SPRITES = new WidgetSprites(
ResourceLocation.fromNamespaceAndPath(CraftGR.MOD_ID, "config"),
ResourceLocation.fromNamespaceAndPath(CraftGR.MOD_ID, "config_disabled"),
ResourceLocation.fromNamespaceAndPath(CraftGR.MOD_ID, "config_highlighted")
Identifier.fromNamespaceAndPath(CraftGR.MOD_ID, "config"),
Identifier.fromNamespaceAndPath(CraftGR.MOD_ID, "config_disabled"),
Identifier.fromNamespaceAndPath(CraftGR.MOD_ID, "config_highlighted")
);

private final RadioVolumeSliderButton volumeSlider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.github.kabanfriends.craftgr.CraftGR;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.resources.Identifier;
import org.lwjgl.glfw.GLFW;

public class Keybinds {
Expand All @@ -15,7 +15,7 @@ public class Keybinds {
"key.craftgr.toggle",
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_M,
KeyMapping.Category.register(ResourceLocation.fromNamespaceAndPath("craftgr", "craftgr"))
KeyMapping.Category.register(Identifier.fromNamespaceAndPath("craftgr", "craftgr"))
),
() -> CraftGR.getInstance().getRadio().toggle()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
package io.github.kabanfriends.craftgr.mixin;

import io.github.kabanfriends.craftgr.gui.RadioOptionContainer;
import io.github.kabanfriends.craftgr.util.ThreadLocals;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.OptionsList;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.List;

@Mixin(OptionsList.OptionEntry.class)
@Mixin(OptionsList.Entry.class)
public class MixinOptionsListOptionEntry {

@ModifyArg(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/components/OptionsList$Entry;<init>(Ljava/util/List;Lnet/minecraft/client/gui/screens/Screen;)V"))
private static List<AbstractWidget> craftgr$initOptionEntry(List<AbstractWidget> list) {
Boolean value = ThreadLocals.RADIO_OPTION_CONTAINER_ADDED.get();
if (value == null || list.size() > 1 || value) {
return list;
@Shadow @Final @Mutable
List<OptionsList.OptionInstanceWidget> children;

@Redirect(method = "<init>", at = @At(value = "FIELD", target = "Lnet/minecraft/client/gui/components/OptionsList$Entry;children:Ljava/util/List;", opcode = Opcodes.PUTFIELD))
private void craftgr$addRadioToChildren(OptionsList.Entry instance, List<OptionsList.OptionInstanceWidget> value) {
Boolean added = ThreadLocals.RADIO_OPTION_CONTAINER_ADDED.get();
if (added == null || value.size() > 1 || added) {
this.children = value;
return;
}

ThreadLocals.RADIO_OPTION_CONTAINER_ADDED.set(true);
return List.of(list.get(0), new RadioOptionContainer(0, 0, 150));
this.children = List.copyOf(value);
}
}
Loading