Skip to content

Commit

Permalink
Make config work not static
Browse files Browse the repository at this point in the history
  • Loading branch information
sub-kek committed Aug 29, 2024
1 parent a4b4595 commit 4068ba0
Show file tree
Hide file tree
Showing 13 changed files with 266 additions and 188 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ tasks.shadowJar {
configurations = listOf(project.configurations.shadow.get())
mergeServiceFiles()

fun relocate(pkg: String) = relocate(pkg, "$group.customdiscs.libs.$pkg") {
fun relocate(pkg: String) = relocate(pkg, "${rootProject.group}.customdiscs.libs.$pkg") {
exclude("com/sedmelluq/discord/lavaplayer/natives/**")
}

Expand Down
63 changes: 52 additions & 11 deletions src/main/java/io/github/subkek/customdiscs/CustomDiscs.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPIBukkitConfig;
import io.github.subkek.customdiscs.command.CustomDiscsCommand;
import io.github.subkek.customdiscs.config.CustomDiscsConfiguration;
import io.github.subkek.customdiscs.config.CDConfig;
import io.github.subkek.customdiscs.event.HopperHandler;
import io.github.subkek.customdiscs.event.JukeboxHandler;
import io.github.subkek.customdiscs.language.YamlLanguage;
Expand All @@ -22,22 +22,28 @@
import lombok.Getter;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.block.Jukebox;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;

public class CustomDiscs extends JavaPlugin {
public static final String PLUGIN_ID = "CustomDiscs";
private VoicePlugin voicechatPlugin;
@Getter
private YamlLanguage language = null;
private YamlLanguage language = new YamlLanguage();
@Getter
private CDConfig cDConfig = new CDConfig(
new File(getDataFolder().getPath(), "config.yml"));
@Getter
private FoliaLib foliaLib = new FoliaLib(this);
@Getter
private BukkitAudiences audience;
public int discsPlayed = 0;

public static CustomDiscs getPlugin() {
return getPlugin(CustomDiscs.class);
Expand All @@ -52,18 +58,16 @@ public void onLoad() {
public void onEnable() {
audience = BukkitAudiences.create(this);

if (getDataFolder().mkdir()) getLogger().info("Created plugin data folder");

CustomDiscsConfiguration.load();
if (getDataFolder().mkdir()) info("Created plugin data folder");

language = new YamlLanguage();
cDConfig.init();
language.init();

linkBStats();

File musicData = new File(this.getDataFolder(), "musicdata");
if (!(musicData.exists())) {
if (musicData.mkdir()) getLogger().info("Created music data folder");
if (musicData.mkdir()) info("Created music data folder");
}

registerVoicechatHook();
Expand Down Expand Up @@ -131,10 +135,10 @@ public void onDisable() {
private void linkBStats() {
BStatsLink bstats = new BStatsLink(getPlugin(), 20077);

bstats.addCustomChart(new BStatsLink.SimplePie("plugin_language", () -> CustomDiscsConfiguration.locale));
bstats.addCustomChart(new BStatsLink.SimplePie("plugin_language", () -> getCDConfig().getLocale()));
bstats.addCustomChart(new BStatsLink.SingleLineChart("discs_played", () -> {
int value = CustomDiscsConfiguration.discsPlayed;
CustomDiscsConfiguration.discsPlayed = 0;
int value = discsPlayed;
discsPlayed = 0;
return value;
}));
}
Expand All @@ -144,7 +148,7 @@ public static void sendMessage(CommandSender sender, Component component) {
}

public static void debug(@NotNull String message, Object... format) {
if (!CustomDiscsConfiguration.debug) return;
if (!getPlugin().getCDConfig().isDebug()) return;
sendMessage(
getPlugin().getServer().getConsoleSender(),
getPlugin().getLanguage().deserialize(
Expand All @@ -156,4 +160,41 @@ public static void debug(@NotNull String message, Object... format) {
)
);
}

public static void info(@NotNull String message, Object... format) {
sendMessage(
getPlugin().getServer().getConsoleSender(),
getPlugin().getLanguage().deserialize(
Formatter.format(
"{0}{1}",
getPlugin().getLanguage().string("prefix.info"),
Formatter.format(message, format)
)
)
);
}

public static void error(@NotNull String message, @Nullable Throwable e, Object... format) {
String stackTrace = "";

if (e != null) {
stackTrace = ExceptionUtils.getStackTrace(e);
}

sendMessage(
getPlugin().getServer().getConsoleSender(),
getPlugin().getLanguage().deserialize(
Formatter.format(
"{0}{1}{2}",
getPlugin().getLanguage().string("prefix.error"),
Formatter.format(message, format),
stackTrace
)
)
);
}

public static void error(@NotNull String message, Object... format) {
error(message, null, format);
}
}
29 changes: 22 additions & 7 deletions src/main/java/io/github/subkek/customdiscs/LavaPlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import de.maxhenkel.voicechat.api.audiochannel.LocationalAudioChannel;
import dev.lavalink.youtube.YoutubeAudioSourceManager;
import dev.lavalink.youtube.http.YoutubeOauth2Handler;
import io.github.subkek.customdiscs.config.CustomDiscsConfiguration;
import io.github.subkek.customdiscs.event.HopperHandler;
import net.kyori.adventure.text.Component;
import org.bukkit.Location;
Expand All @@ -38,7 +37,7 @@ public class LavaPlayerManager {

public LavaPlayerManager() {
YoutubeAudioSourceManager source = new YoutubeAudioSourceManager(false);
if (CustomDiscsConfiguration.oauth2) {
if (plugin.getCDConfig().isYoutubeOauth2()) {
try {
String oauth2token;

Expand Down Expand Up @@ -113,14 +112,30 @@ public void playLocationalAudioYoutube(Block block, VoicechatServerApi api, Stri
lavaPlayer.ytUrl = ytUrl;
lavaPlayer.block = block;
lavaPlayer.playerUUID = uuid;
lavaPlayer.audioChannel = api.createLocationalAudioChannel(UUID.randomUUID(), api.fromServerLevel(block.getWorld()), api.createPosition(block.getLocation().getX() + 0.5d, block.getLocation().getY() + 0.5d, block.getLocation().getZ() + 0.5d));
lavaPlayer.audioChannel = api.createLocationalAudioChannel(
UUID.randomUUID(),
api.fromServerLevel(block.getWorld()),
api.createPosition(
block.getLocation().getX() + 0.5d,
block.getLocation().getY() + 0.5d,
block.getLocation().getZ() + 0.5d
)
);

if (lavaPlayer.audioChannel == null) return;

lavaPlayer.audioChannel.setCategory(VoicePlugin.MUSIC_DISC_CATEGORY);
lavaPlayer.audioChannel.setDistance(CustomDiscsConfiguration.musicDiscDistance);

lavaPlayer.playersInRange = api.getPlayersInRange(api.fromServerLevel(block.getWorld()), api.createPosition(block.getLocation().getX() + 0.5d, block.getLocation().getY() + 0.5d, block.getLocation().getZ() + 0.5d), CustomDiscsConfiguration.musicDiscDistance);
lavaPlayer.audioChannel.setDistance(plugin.getCDConfig().getMusicDiscDistance());

lavaPlayer.playersInRange = api.getPlayersInRange(
api.fromServerLevel(block.getWorld()),
api.createPosition(
block.getLocation().getX() + 0.5d,
block.getLocation().getY() + 0.5d,
block.getLocation().getZ() + 0.5d
),
plugin.getCDConfig().getMusicDiscDistance()
);

lavaPlayer.lavaPlayerThread.start();

Expand Down Expand Up @@ -237,7 +252,7 @@ public void loadFailed(FriendlyException e) {
return;
}

int volume = Math.round(CustomDiscsConfiguration.musicDiscVolume * 100);
int volume = Math.round(plugin.getCDConfig().getMusicDiscVolume() * 100);
audioPlayer.setVolume(volume);

long start = 0L;
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/io/github/subkek/customdiscs/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import de.maxhenkel.voicechat.api.VoicechatServerApi;
import de.maxhenkel.voicechat.api.audiochannel.AudioPlayer;
import de.maxhenkel.voicechat.api.audiochannel.LocationalAudioChannel;
import io.github.subkek.customdiscs.config.CustomDiscsConfiguration;
import io.github.subkek.customdiscs.event.HopperHandler;
import javazoom.spi.mpeg.sampled.convert.MpegFormatConversionProvider;
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
Expand Down Expand Up @@ -59,14 +58,30 @@ public void playLocationalAudio(Path soundFilePath, Block block, Component actio
discPlayer.block = block;
discPlayer.soundFilePath = soundFilePath;
discPlayer.playerUUID = uuid;
discPlayer.audioChannel = api.createLocationalAudioChannel(uuid, api.fromServerLevel(block.getWorld()), api.createPosition(block.getLocation().getX() + 0.5d, block.getLocation().getY() + 0.5d, block.getLocation().getZ() + 0.5d));
discPlayer.audioChannel = api.createLocationalAudioChannel(
uuid,
api.fromServerLevel(block.getWorld()),
api.createPosition(
block.getLocation().getX() + 0.5d,
block.getLocation().getY() + 0.5d,
block.getLocation().getZ() + 0.5d
)
);

if (discPlayer.audioChannel == null) return;

discPlayer.audioChannel.setCategory(VoicePlugin.MUSIC_DISC_CATEGORY);
discPlayer.audioChannel.setDistance(CustomDiscsConfiguration.musicDiscDistance);

discPlayer.playersInRange = api.getPlayersInRange(api.fromServerLevel(block.getWorld()), api.createPosition(block.getLocation().getX() + 0.5d, block.getLocation().getY() + 0.5d, block.getLocation().getZ() + 0.5d), CustomDiscsConfiguration.musicDiscDistance);
discPlayer.audioChannel.setDistance(plugin.getCDConfig().getMusicDiscDistance());

discPlayer.playersInRange = api.getPlayersInRange(
api.fromServerLevel(block.getWorld()),
api.createPosition(
block.getLocation().getX() + 0.5d,
block.getLocation().getY() + 0.5d,
block.getLocation().getZ() + 0.5d
),
plugin.getCDConfig().getMusicDiscDistance()
);

discPlayer.audioPlayerThread.start();

Expand Down Expand Up @@ -121,11 +136,10 @@ private byte[] convertFormat(Path file) throws UnsupportedAudioFileException, IO

assert finalInputStream != null;

return adjustVolume(finalInputStream.readAllBytes(), CustomDiscsConfiguration.musicDiscVolume);
return adjustVolume(finalInputStream.readAllBytes(), plugin.getCDConfig().getMusicDiscVolume());
}

private byte[] adjustVolume(byte[] audioSamples, double volume) {

if (volume > 1d || volume < 0d) {
plugin.getServer().getLogger().severe("The volume must be between 0 and 1 in the config!");
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.github.subkek.customdiscs.Keys;
import io.github.subkek.customdiscs.LegacyUtil;
import io.github.subkek.customdiscs.command.AbstractSubCommand;
import io.github.subkek.customdiscs.config.CustomDiscsConfiguration;
import net.kyori.adventure.platform.bukkit.BukkitComponentSerializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
Expand Down Expand Up @@ -117,8 +116,8 @@ private void executePlayer(Player player, CommandArguments arguments) {
.build();
meta.addItemFlags(ItemFlag.values());
meta.setLore(List.of(BukkitComponentSerializer.legacy().serialize(customLoreSong)));
if (CustomDiscsConfiguration.useCustomModelData)
meta.setCustomModelData(CustomDiscsConfiguration.customModelData);
if (plugin.getCDConfig().isUseCustomModelData())
meta.setCustomModelData(plugin.getCDConfig().getCustomModelData());

PersistentDataContainer data = meta.getPersistentDataContainer();
NamespacedKey discYtMeta = Keys.YOUTUBE_DISC.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.github.subkek.customdiscs.Keys;
import io.github.subkek.customdiscs.LegacyUtil;
import io.github.subkek.customdiscs.command.AbstractSubCommand;
import io.github.subkek.customdiscs.config.CustomDiscsConfiguration;
import net.kyori.adventure.platform.bukkit.BukkitComponentSerializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
Expand Down Expand Up @@ -84,8 +83,8 @@ public void executePlayer(Player player, CommandArguments arguments) {
.build();
meta.addItemFlags(ItemFlag.values());
meta.setLore(List.of(BukkitComponentSerializer.legacy().serialize(customLoreSong)));
if (CustomDiscsConfiguration.useCustomModelData)
meta.setCustomModelData(CustomDiscsConfiguration.customModelData);
if (plugin.getCDConfig().isUseCustomModelDataYoutube())
meta.setCustomModelData(plugin.getCDConfig().getCustomModelDataYoutube());

String youtubeUrl = getArgumentValue(arguments, "url", String.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import dev.jorel.commandapi.executors.CommandArguments;
import io.github.subkek.customdiscs.CustomDiscs;
import io.github.subkek.customdiscs.command.AbstractSubCommand;
import io.github.subkek.customdiscs.config.CustomDiscsConfiguration;
import org.apache.commons.io.FileUtils;
import org.bukkit.command.CommandSender;

Expand Down Expand Up @@ -60,7 +59,8 @@ public void execute(CommandSender sender, CommandArguments arguments) {
return;
}

if (!getFileExtension(filename).equals("wav") && !getFileExtension(filename).equals("mp3") && !getFileExtension(filename).equals("flac")) {
if (!getFileExtension(filename).equals("wav") && !getFileExtension(filename).equals("mp3") &&
!getFileExtension(filename).equals("flac")) {
CustomDiscs.sendMessage(sender, plugin.getLanguage().PComponent("unknown-extension-error"));
return;
}
Expand All @@ -73,16 +73,18 @@ public void execute(CommandSender sender, CommandArguments arguments) {

if (connection != null) {
long size = connection.getContentLengthLong() / 1048576;
if (size > CustomDiscsConfiguration.maxDownloadSize) {
CustomDiscs.sendMessage(sender, plugin.getLanguage().PComponent("download-file-large-error", String.valueOf(CustomDiscsConfiguration.maxDownloadSize)));
if (size > plugin.getCDConfig().getMaxDownloadSize()) {
CustomDiscs.sendMessage(sender, plugin.getLanguage().PComponent("download-file-large-error",
String.valueOf(plugin.getCDConfig().getMaxDownloadSize())));
return;
}
}

FileUtils.copyURLToFile(fileURL, downloadFile);

CustomDiscs.sendMessage(sender, plugin.getLanguage().PComponent("download-successful"));
CustomDiscs.sendMessage(sender, plugin.getLanguage().PComponent("create-disc-tooltip", plugin.getLanguage().string("create-command-syntax")));
CustomDiscs.sendMessage(sender, plugin.getLanguage().PComponent("create-disc-tooltip",
plugin.getLanguage().string("create-command-syntax")));
} catch (Throwable e) {
plugin.getLogger().log(Level.SEVERE, "Error while download music: ", e);
CustomDiscs.sendMessage(sender, plugin.getLanguage().PComponent("download-error"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import dev.jorel.commandapi.executors.CommandArguments;
import io.github.subkek.customdiscs.CustomDiscs;
import io.github.subkek.customdiscs.command.AbstractSubCommand;
import io.github.subkek.customdiscs.config.CustomDiscsConfiguration;
import org.bukkit.command.CommandSender;

public class ReloadSubCommand extends AbstractSubCommand {
Expand Down Expand Up @@ -39,7 +38,7 @@ public void execute(CommandSender sender, CommandArguments arguments) {
return;
}

CustomDiscsConfiguration.load();
plugin.getCDConfig().init();
plugin.getLanguage().init();
CustomDiscs.sendMessage(sender, plugin.getLanguage().PComponent("config-reloaded"));
}
Expand Down
Loading

0 comments on commit 4068ba0

Please sign in to comment.