Skip to content

Commit

Permalink
Switch to CommandAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
sub-kek committed Aug 29, 2024
1 parent 6abe2ac commit fce303e
Show file tree
Hide file tree
Showing 29 changed files with 564 additions and 649 deletions.
121 changes: 82 additions & 39 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import net.minecrell.pluginyml.bukkit.BukkitPluginDescription.Permission.Default as PermDefault

plugins {
id("java-library")
id("io.github.kota65535.dependency-report") version "2.0.1"
id("com.github.johnrengelman.shadow") version ("8.1.1")
id("io.github.goooler.shadow") version "8.1.8"
id("net.minecrell.plugin-yml.bukkit") version "0.6.0"
}

allprojects {
group = "io.github.subkek.customdiscs"
version = "1.5.1"
group = "io.github.subkek"
version = properties["plugin_version"]!!
}

java.sourceCompatibility = JavaVersion.VERSION_16
java.targetCompatibility = JavaVersion.VERSION_16
java.disableAutoTargetJvm()

repositories {
mavenCentral()
maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/")
Expand Down Expand Up @@ -47,6 +45,8 @@ dependencies {
exclude("org.slf4j")
}

shadow("dev.jorel:commandapi-bukkit-shade:9.5.3")

shadow(platform("net.kyori:adventure-bom:4.17.0"))
shadow("net.kyori:adventure-api")
shadow("net.kyori:adventure-text-minimessage")
Expand All @@ -61,6 +61,55 @@ dependencies {
annotationProcessor("org.projectlombok:lombok:1.18.34")
}

val pluginId = properties["plugin_id"]

bukkit {
name = rootProject.name
version = rootProject.version as String
main = "io.github.subkek.customdiscs.CustomDiscs"

authors = listOf("subkek")
website = "https://discord.gg/eRvwvmEXWz"
apiVersion = "1.16"

foliaSupported = true

permissions {
register("$pluginId.help") {
default = PermDefault.TRUE
}
register("$pluginId.reload") {
default = PermDefault.OP
}
register("$pluginId.download") {
default = PermDefault.TRUE
}
register("$pluginId.create") {
default = PermDefault.TRUE
}
register("$pluginId.createyt") {
default = PermDefault.TRUE
}
register("$pluginId.play") {
default = PermDefault.TRUE
}
register("$pluginId.playt") {
default = PermDefault.TRUE
}
}

depend = listOf(
"voicechat",
"ProtocolLib"
)
}

java {
sourceCompatibility = JavaVersion.VERSION_16
targetCompatibility = JavaVersion.VERSION_16
disableAutoTargetJvm()
}

tasks.jar {
enabled = false
}
Expand All @@ -75,38 +124,32 @@ tasks.shadowJar {
configurations = listOf(project.configurations.shadow.get())
mergeServiceFiles()

relocate("org.apache", "io.github.subkek.customdiscs.libs.org.apache")
relocate("org.jsoup", "io.github.subkek.customdiscs.libs.org.jsoup")
relocate("com.fasterxml", "io.github.subkek.customdiscs.libs.com.fasterxml")
relocate("org.yaml.snakeyaml", "io.github.subkek.customdiscs.libs.org.yaml.snakeyaml")
relocate("org.simpleyaml", "io.github.subkek.customdiscs.libs.org.simpleyaml")
relocate("org.jflac", "io.github.subkek.customdiscs.libs.org.jflac")
relocate("org.json", "io.github.subkek.customdiscs.libs.org.json")
//relocate("org.mozilla", "io.github.subkek.customdiscs.libs.org.mozilla")
relocate("org.tritonus", "io.github.subkek.customdiscs.libs.org.tritonus")
relocate("mozilla", "io.github.subkek.customdiscs.libs.mozilla")
relocate("junit", "io.github.subkek.customdiscs.libs.junit")
relocate("javazoom", "io.github.subkek.customdiscs.libs.javazoom")
relocate("certificates", "io.github.subkek.customdiscs.libs.certificates")
relocate("org.hamcrest", "io.github.subkek.customdiscs.libs.org.hamcrest")
relocate("org.junit", "io.github.subkek.customdiscs.libs.org.junit")
relocate("net.sourceforge.jaad.aac", "io.github.subkek.customdiscs.libs.net.sourceforge.jaad.aac")
relocate("net.kyori", "io.github.subkek.customdiscs.libs.net.kyori")
relocate("net.iharder", "io.github.subkek.customdiscs.libs.net.iharder")
relocate("com.tcoded", "io.github.subkek.customdiscs.libs.com.tcoded")
relocate("com.grack", "io.github.subkek.customdiscs.libs.com.grack")
relocate("dev.lavalink", "io.github.subkek.customdiscs.libs.dev.lavalink")
relocate("org.intellij", "io.github.subkek.customdiscs.libs.org.intellij")
relocate("org.jetbrains", "io.github.subkek.customdiscs.libs.org.jetbrains")
relocate("com.sedmelluq", "io.github.subkek.customdiscs.libs.com.sedmelluq") {
fun relocate(pkg: String) = relocate(pkg, "$group.customdiscs.libs.$pkg") {
exclude("com/sedmelluq/discord/lavaplayer/natives/**")
}

//minimize()
}

tasks.processResources {
filesMatching("plugin.yml") {
expand(project.properties)
}
relocate("org.apache")
relocate("org.jsoup")
relocate("com.fasterxml")
relocate("org.yaml.snakeyaml")
relocate("org.simpleyaml")
relocate("org.jflac")
relocate("org.json")
relocate("org.tritonus")
relocate("mozilla")
relocate("junit")
relocate("javazoom")
relocate("certificates")
relocate("org.hamcrest")
relocate("org.junit")
relocate("net.sourceforge.jaad.aac")
relocate("net.kyori")
relocate("net.iharder")
relocate("com.tcoded")
relocate("com.grack")
relocate("dev.lavalink")
relocate("org.intellij")
relocate("org.jetbrains")
relocate("com.sedmelluq")
relocate("dev.jorel.commandapi")
}
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
plugin_version = 2.0.0
plugin_id = customdiscs
49 changes: 27 additions & 22 deletions src/main/java/io/github/subkek/customdiscs/CustomDiscs.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import com.comphenix.protocol.events.PacketEvent;
import com.tcoded.folialib.FoliaLib;
import de.maxhenkel.voicechat.api.BukkitVoicechatService;
import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPIBukkitConfig;
import io.github.subkek.customdiscs.command.CustomDiscsCommand;
import io.github.subkek.customdiscs.command.CustomDiscsTabCompleter;
import io.github.subkek.customdiscs.config.CustomDiscsConfiguration;
import io.github.subkek.customdiscs.event.HopperHandler;
import io.github.subkek.customdiscs.event.JukeboxHandler;
Expand All @@ -24,13 +25,12 @@
import org.bukkit.block.Jukebox;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.io.File;

public class CustomDiscs extends JavaPlugin {
public static final String PLUGIN_ID = "CustomDiscs";
@Getter
private static CustomDiscs instance = null;
private VoicePlugin voicechatPlugin;
@Getter
private YamlLanguage language = null;
Expand All @@ -39,22 +39,18 @@ public class CustomDiscs extends JavaPlugin {
@Getter
private BukkitAudiences audience;

public static void debug(String message, String... format) {
if (!CustomDiscsConfiguration.debug) return;

CustomDiscs plugin = getInstance();
public static CustomDiscs getPlugin() {
return getPlugin(CustomDiscs.class);
}

plugin.getAudience().sender(plugin.getServer().getConsoleSender())
.sendMessage(plugin.getLanguage().deserialize(
Formatter.format("<yellow>[CustomDiscs Debug] {0}", message),
format
));
@Override
public void onLoad() {
CommandAPI.onLoad(new CommandAPIBukkitConfig(this).verboseOutput(true));
new CustomDiscsCommand().register("customdiscs");
}

@Override
public void onEnable() {
CustomDiscs.instance = this;

audience = BukkitAudiences.create(this);

if (getDataFolder().mkdir()) getLogger().info("Created plugin data folder");
Expand Down Expand Up @@ -84,11 +80,6 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new JukeboxHandler(), this);
getServer().getPluginManager().registerEvents(HopperHandler.getInstance(), this);

CustomDiscsCommand customDiscsCommand = new CustomDiscsCommand();
getCommand("customdisc").setExecutor(customDiscsCommand);
getCommand("customdisc").setTabCompleter(new CustomDiscsTabCompleter(customDiscsCommand));


ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
protocolManager.addPacketListener(new PacketAdapter(this, ListenerPriority.NORMAL, PacketType.Play.Server.WORLD_EVENT) {
@Override
Expand Down Expand Up @@ -126,7 +117,7 @@ public void onDisable() {
}

private void linkBStats() {
BStatsLink bstats = new BStatsLink(getInstance(), 20077);
BStatsLink bstats = new BStatsLink(getPlugin(), 20077);

bstats.addCustomChart(new BStatsLink.SimplePie("plugin_language", () -> CustomDiscsConfiguration.locale));
bstats.addCustomChart(new BStatsLink.SingleLineChart("discs_played", () -> {
Expand All @@ -136,7 +127,21 @@ private void linkBStats() {
}));
}

public void sendMessage(CommandSender sender, Component component) {
audience.sender(sender).sendMessage(component);
public static void sendMessage(CommandSender sender, Component component) {
getPlugin().getAudience().sender(sender).sendMessage(component);
}

public static void debug(@NotNull String message, Object... format) {
if (!CustomDiscsConfiguration.debug) return;
sendMessage(
getPlugin().getServer().getConsoleSender(),
getPlugin().getLanguage().deserialize(
Formatter.format(
"{0}{1}",
getPlugin().getLanguage().string("prefix.debug"),
Formatter.format(message, format)
)
)
);
}
}
2 changes: 1 addition & 1 deletion src/main/java/io/github/subkek/customdiscs/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class Key<T> {
private final PersistentDataType<T, T> dataType;

public Key(String key, PersistentDataType<T, T> dataType) {
this.key = new NamespacedKey(CustomDiscs.getInstance(), key);
this.key = new NamespacedKey(CustomDiscs.getPlugin(), key);
this.dataType = dataType;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

public class LavaPlayerManager {
private static LavaPlayerManager instance;
private final CustomDiscs plugin = CustomDiscs.getInstance();
private final CustomDiscs plugin = CustomDiscs.getPlugin();
private final AudioPlayerManager lavaPlayerManager = new DefaultAudioPlayerManager();
private final Map<UUID, LavaPlayer> playerMap = new HashMap<>();
File refreshTokenFile = new File(plugin.getDataFolder(), ".youtube-token");
Expand Down Expand Up @@ -205,7 +205,7 @@ public void noMatches() {
playerUUID.toString(), ytUrl);
for (ServerPlayer serverPlayer : playersInRange) {
Player bukkitPlayer = (Player) serverPlayer.getPlayer();
plugin.sendMessage(bukkitPlayer, plugin.getLanguage().PComponent("url-no-matches-error"));
CustomDiscs.sendMessage(bukkitPlayer, plugin.getLanguage().PComponent("url-no-matches-error"));
}
stopPlaying(playerUUID, true);
}
Expand All @@ -217,7 +217,7 @@ public void loadFailed(FriendlyException e) {
playerUUID.toString(), ytUrl, e.getMessage());
for (ServerPlayer serverPlayer : playersInRange) {
Player bukkitPlayer = (Player) serverPlayer.getPlayer();
plugin.sendMessage(bukkitPlayer, plugin.getLanguage().PComponent("audio-load-error"));
CustomDiscs.sendMessage(bukkitPlayer, plugin.getLanguage().PComponent("audio-load-error"));
}
stopPlaying(playerUUID, true);
}
Expand Down Expand Up @@ -271,12 +271,10 @@ public void loadFailed(FriendlyException e) {
} catch (Throwable e) {
for (ServerPlayer serverPlayer : playersInRange) {
Player bukkitPlayer = (Player) serverPlayer.getPlayer();
plugin.sendMessage(bukkitPlayer, plugin.getLanguage().PComponent("disc-play-error"));
CustomDiscs.sendMessage(bukkitPlayer, plugin.getLanguage().PComponent("disc-play-error"));
plugin.getLogger().log(Level.SEVERE, "Error while playing disc: ", e);
}
}
}


}
}
8 changes: 3 additions & 5 deletions src/main/java/io/github/subkek/customdiscs/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class PlayerManager {
false
);
private static PlayerManager instance;
private final CustomDiscs plugin = CustomDiscs.getInstance();
private final CustomDiscs plugin = CustomDiscs.getPlugin();
private final Map<UUID, DiscPlayer> playerMap = new HashMap<>();

public static PlayerManager getInstance() {
Expand Down Expand Up @@ -87,7 +87,7 @@ private AudioPlayer playChannel(DiscPlayer discPlayer) {
} catch (Exception e) {
for (ServerPlayer serverPlayer : discPlayer.playersInRange) {
Player bukkitPlayer = (Player) serverPlayer.getPlayer();
plugin.sendMessage(bukkitPlayer, plugin.getLanguage().PComponent("disc-play-error"));
CustomDiscs.sendMessage(bukkitPlayer, plugin.getLanguage().PComponent("disc-play-error"));
}
return null;
}
Expand Down Expand Up @@ -228,14 +228,12 @@ private void startTrackJob() {
} catch (Throwable e) {
for (ServerPlayer serverPlayer : playersInRange) {
Player bukkitPlayer = (Player) serverPlayer.getPlayer();
plugin.sendMessage(bukkitPlayer, plugin.getLanguage().PComponent("disc-play-error"));
CustomDiscs.sendMessage(bukkitPlayer, plugin.getLanguage().PComponent("disc-play-error"));
plugin.getLogger().log(Level.SEVERE, "Error while playing disc: ", e);
}
}
}

private final Thread audioPlayerThread = new Thread(this::startTrackJob, "AudioPlayerThread");


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private int[][] getMusicDiscIcon() {
return image;
}
} catch (Throwable e) {
CustomDiscs.getInstance().getLogger().log(Level.SEVERE, "Error getting music discs icon");
CustomDiscs.getPlugin().getLogger().log(Level.SEVERE, "Error getting music discs icon");
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.subkek.customdiscs.command;

import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.executors.CommandArguments;
import io.github.subkek.customdiscs.util.Formatter;
import org.bukkit.command.CommandSender;

public abstract class AbstractSubCommand extends CommandAPICommand {
public AbstractSubCommand(String commandName) {
super(commandName);
}

protected <T>T getArgumentValue(CommandArguments arguments, String nodeName, Class<T> argumentType) {
T value;
if ((value = arguments.getByClass(nodeName, argumentType)) == null)
throw new IllegalArgumentException(Formatter.format(
"Couldn't find argument {0} with name", nodeName
));
return value;
}

public abstract String getDescription();
public abstract String getSyntax();
public abstract boolean hasPermission(CommandSender sender);
}
Loading

0 comments on commit fce303e

Please sign in to comment.