diff --git a/build.gradle b/build.gradle index a3c1963..cb31060 100644 --- a/build.gradle +++ b/build.gradle @@ -26,9 +26,11 @@ dependencies { // Use this dependency if you don't want to compile bukkit implementation "io.papermc.paper:paper-api:${bukkit_version}" implementation "de.maxhenkel.voicechat:voicechat-api:${voicechat_api_version}" + } repositories { + maven { url "https://repo.dmulloy2.net/repository/public/" } mavenCentral() maven { name = "henkelmax.public" diff --git a/gradle.properties b/gradle.properties index d5823a9..7264ae1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,5 +9,5 @@ bukkit_version=1.19-R0.1-SNAPSHOT voicechat_api_version=2.2.22 plugin_version=1.0.0 -maven_group=na.Navoei.customdiscsplugin +maven_group=me.Navoei.customdiscsplugin archives_base_name=custom-discs \ No newline at end of file diff --git a/src/main/java/na/Navoei/customdiscsplugin/CustomDiscs.java b/src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java similarity index 84% rename from src/main/java/na/Navoei/customdiscsplugin/CustomDiscs.java rename to src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java index 583de33..219f8b4 100644 --- a/src/main/java/na/Navoei/customdiscsplugin/CustomDiscs.java +++ b/src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java @@ -1,14 +1,14 @@ -package na.Navoei.customdiscsplugin; +package me.Navoei.customdiscsplugin; import de.maxhenkel.voicechat.api.BukkitVoicechatService; -import na.Navoei.customdiscsplugin.command.CustomDisc; +import me.Navoei.customdiscsplugin.command.CustomDisc; +import me.Navoei.customdiscsplugin.event.JukeBox; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bukkit.plugin.java.JavaPlugin; import javax.annotation.Nullable; import java.io.File; -import java.net.URI; public final class CustomDiscs extends JavaPlugin { @@ -17,7 +17,7 @@ public final class CustomDiscs extends JavaPlugin { static CustomDiscs instance; @Nullable - private PlayMusic voicechatPlugin; + private VoicePlugin voicechatPlugin; @Override public void onEnable() { @@ -39,14 +39,14 @@ public final class CustomDiscs extends JavaPlugin { } if (service != null) { - voicechatPlugin = new PlayMusic(); + voicechatPlugin = new VoicePlugin(); service.registerPlugin(voicechatPlugin); LOGGER.info("Successfully registered CustomDiscs plugin"); } else { LOGGER.info("Failed to register CustomDiscs plugin"); } - + getServer().getPluginManager().registerEvents(new JukeBox(), this); getCommand("customdisc").setExecutor(command); } diff --git a/src/main/java/na/Navoei/customdiscsplugin/PlayMusic.java b/src/main/java/me/Navoei/customdiscsplugin/VoicePlugin.java similarity index 52% rename from src/main/java/na/Navoei/customdiscsplugin/PlayMusic.java rename to src/main/java/me/Navoei/customdiscsplugin/VoicePlugin.java index de656c8..4ca073b 100644 --- a/src/main/java/na/Navoei/customdiscsplugin/PlayMusic.java +++ b/src/main/java/me/Navoei/customdiscsplugin/VoicePlugin.java @@ -1,10 +1,19 @@ -package na.Navoei.customdiscsplugin; +package me.Navoei.customdiscsplugin; import de.maxhenkel.voicechat.api.VoicechatApi; import de.maxhenkel.voicechat.api.VoicechatPlugin; +import de.maxhenkel.voicechat.api.VoicechatServerApi; import de.maxhenkel.voicechat.api.events.EventRegistration; +import de.maxhenkel.voicechat.api.events.VoicechatServerStartedEvent; -public class PlayMusic implements VoicechatPlugin { +import javax.annotation.Nullable; + +public class VoicePlugin implements VoicechatPlugin { + + public static VoicechatApi voicechatApi; + + @Nullable + public static VoicechatServerApi voicechatServerApi; /** * @return the unique ID for this voice chat plugin @@ -21,7 +30,7 @@ public class PlayMusic implements VoicechatPlugin { */ @Override public void initialize(final VoicechatApi api) { - + VoicePlugin.voicechatApi = api; } /** @@ -31,7 +40,14 @@ public class PlayMusic implements VoicechatPlugin { */ @Override public void registerEvents(final EventRegistration registration) { - + registration.registerEvent(VoicechatServerStartedEvent.class, this::onServerStarted); } + public void onServerStarted(final VoicechatServerStartedEvent event) { + VoicePlugin.voicechatServerApi = event.getVoicechat(); + } + + public void playAudio() { + + } } diff --git a/src/main/java/na/Navoei/customdiscsplugin/command/CustomDisc.java b/src/main/java/me/Navoei/customdiscsplugin/command/CustomDisc.java similarity index 96% rename from src/main/java/na/Navoei/customdiscsplugin/command/CustomDisc.java rename to src/main/java/me/Navoei/customdiscsplugin/command/CustomDisc.java index 28a36d4..43f74ee 100644 --- a/src/main/java/na/Navoei/customdiscsplugin/command/CustomDisc.java +++ b/src/main/java/me/Navoei/customdiscsplugin/command/CustomDisc.java @@ -1,16 +1,18 @@ -package na.Navoei.customdiscsplugin.command; +package me.Navoei.customdiscsplugin.command; -import com.google.common.io.Files; -import na.Navoei.customdiscsplugin.CustomDiscs; +import me.Navoei.customdiscsplugin.CustomDiscs; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextDecoration; import org.bukkit.ChatColor; import org.bukkit.Material; +import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeModifier; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemFlag; @@ -98,6 +100,7 @@ public class CustomDisc implements CommandExecutor { itemLore.add(customLoreFile); meta.lore(itemLore); meta.addItemFlags(ItemFlag.values()); + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); p.getInventory().getItemInMainHand().setItemMeta(meta); diff --git a/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java b/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java new file mode 100644 index 0000000..9f9f5e4 --- /dev/null +++ b/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java @@ -0,0 +1,75 @@ +package me.Navoei.customdiscsplugin.event; + +import me.Navoei.customdiscsplugin.CustomDiscs; +import me.Navoei.customdiscsplugin.VoicePlugin; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemFlag; + +import javax.sound.sampled.UnsupportedAudioFileException; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Objects; + +public class JukeBox implements Listener { + + @EventHandler + public void onInsert(PlayerInteractEvent event) throws UnsupportedAudioFileException, IOException { + + if (event.getAction().isRightClick() && isCustomMusicDisc(event.getPlayer()) && Objects.requireNonNull(event.getClickedBlock()).getType().equals(Material.JUKEBOX)) { + + Component soundFileComponent = Objects.requireNonNull(event.getPlayer().getInventory().getItemInMainHand().getItemMeta().lore()).get(1).asComponent(); + String soundFileName = PlainTextComponentSerializer.plainText().serialize(soundFileComponent); + + Path soundFilePath = Path.of(CustomDiscs.getInstance().getDataFolder() + "\\musicdata\\" + soundFileName); + if (soundFilePath.toFile().exists()) { + Component songNameComponent = Objects.requireNonNull(event.getPlayer().getInventory().getItemInMainHand().getItemMeta().lore()).get(0).asComponent(); + String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent); + event.getPlayer().sendMessage(ChatColor.GOLD + "Now playing: " + songName); + + //VoicePlugin.voicechatServerApi.createAudioPlayer(); + + } else { + event.getPlayer().sendMessage(ChatColor.RED + "Sound file not found."); + event.setCancelled(true); + throw new FileNotFoundException("ERROR: Sound file is missing!"); + } + } + + } + + public boolean isCustomMusicDisc(Player p) { + + if ( + p.getInventory().getItemInMainHand().hasItemFlag(ItemFlag.HIDE_ENCHANTS) && + ( + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_13) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_CAT) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_BLOCKS) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_CHIRP) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_FAR) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_MALL) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_MELLOHI) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_STAL) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_STRAD) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_WARD) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_11) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_WAIT) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_OTHERSIDE) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_5) || + p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_PIGSTEP) + ) + ) { + return true; + } + return false; + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 72cdfef..ce2cef3 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: CustomDiscs version: ${version} -main: na.Navoei.customdiscsplugin.CustomDiscs +main: me.Navoei.customdiscsplugin.CustomDiscs api-version: ${bukkit_api_version} prefix: CustomDiscs authors: [ "Navoei" ]