diff --git a/src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java b/src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java index ad64a2f..446ba79 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java +++ b/src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java @@ -11,6 +11,7 @@ import de.maxhenkel.voicechat.api.BukkitVoicechatService; import dev.jorel.commandapi.CommandAPI; import dev.jorel.commandapi.CommandAPIBukkitConfig; import me.Navoei.customdiscsplugin.command.CustomDiscCommand; +import me.Navoei.customdiscsplugin.event.GoatHorn; import me.Navoei.customdiscsplugin.event.JukeBox; import me.Navoei.customdiscsplugin.language.Lang; import org.bukkit.NamespacedKey; @@ -73,6 +74,7 @@ public final class CustomDiscs extends JavaPlugin { } getServer().getPluginManager().registerEvents(new JukeBox(), this); + getServer().getPluginManager().registerEvents(new GoatHorn(), this); getServer().getPluginManager().registerEvents(new HopperManager(), this); musicDiscDistance = Objects.requireNonNull(getConfig().getInt("music-disc-distance")); @@ -100,7 +102,7 @@ public final class CustomDiscs extends JavaPlugin { //Start the jukebox state manager. //This keeps the jukebox powered while custom song is playing, - //which perfectly emulated the vanilla behavior of discs. + //which perfectly emulates the vanilla behavior of discs. JukeboxStateManager.start(jukebox); } } diff --git a/src/main/java/me/Navoei/customdiscsplugin/PlayerManager.java b/src/main/java/me/Navoei/customdiscsplugin/PlayerManager.java index 672f2e4..82cbfd5 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/PlayerManager.java +++ b/src/main/java/me/Navoei/customdiscsplugin/PlayerManager.java @@ -100,8 +100,7 @@ public class PlayerManager { } }); } - - /* + public void playLocationalAudioHorn(VoicechatServerApi api, Path soundFilePath, Player block, Component actionbarComponent, float range) { UUID id = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes()); @@ -156,7 +155,6 @@ public class PlayerManager { } }); } - */ @Nullable private de.maxhenkel.voicechat.api.audiochannel.AudioPlayer playChannel(VoicechatServerApi api, AudioChannel audioChannel, Block block, Path soundFilePath, Collection playersInRange) { @@ -175,8 +173,7 @@ public class PlayerManager { return null; } } - - /* + @Nullable private de.maxhenkel.voicechat.api.audiochannel.AudioPlayer playChannelHorn(VoicechatServerApi api, AudioChannel audioChannel, Player block, Path soundFilePath, Collection playersInRange) { try { @@ -194,7 +191,6 @@ public class PlayerManager { return null; } } - */ private static short[] readSoundFile(Path file) throws UnsupportedAudioFileException, IOException, LineUnavailableException { return VoicePlugin.voicechatApi.getAudioConverter().bytesToShorts(convertFormat(file, FORMAT)); diff --git a/src/main/java/me/Navoei/customdiscsplugin/VoicePlugin.java b/src/main/java/me/Navoei/customdiscsplugin/VoicePlugin.java index 008de83..c631a6d 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/VoicePlugin.java +++ b/src/main/java/me/Navoei/customdiscsplugin/VoicePlugin.java @@ -78,6 +78,7 @@ public class VoicePlugin implements VoicechatPlugin { .build(); voicechatServerApi.registerVolumeCategory(goatHorns); + /* playerHeads = voicechatServerApi.volumeCategoryBuilder() .setId(PLAYER_HEAD_CATEGORY) .setName("Player Heads") @@ -85,6 +86,7 @@ public class VoicePlugin implements VoicechatPlugin { .setIcon(getPlayerHeadsIcon()) .build(); voicechatServerApi.registerVolumeCategory(playerHeads); + */ } @@ -141,7 +143,8 @@ public class VoicePlugin implements VoicechatPlugin { } return null; } - + + /* private int[][] getPlayerHeadsIcon() { try { Enumeration resources = CustomDiscs.getInstance().getClass().getClassLoader().getResources("player_head_category.png"); @@ -168,5 +171,7 @@ public class VoicePlugin implements VoicechatPlugin { } return null; } + */ + } \ No newline at end of file diff --git a/src/main/java/me/Navoei/customdiscsplugin/event/GoatHorn.java b/src/main/java/me/Navoei/customdiscsplugin/event/GoatHorn.java new file mode 100644 index 0000000..0b533a2 --- /dev/null +++ b/src/main/java/me/Navoei/customdiscsplugin/event/GoatHorn.java @@ -0,0 +1,104 @@ +package me.Navoei.customdiscsplugin.event; + +import me.Navoei.customdiscsplugin.CustomDiscs; +import me.Navoei.customdiscsplugin.PlayerManager; +import me.Navoei.customdiscsplugin.VoicePlugin; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.block.Block; +import org.bukkit.block.TileState; +import org.bukkit.block.data.Powerable; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.NotNull; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Objects; + +public class GoatHorn implements Listener { + + static CustomDiscs customDiscs = CustomDiscs.getInstance(); + PlayerManager playerManager = PlayerManager.instance(); + + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerInteract(PlayerInteractEvent event) throws IOException { + ItemStack item = event.getItem(); + Player player = event.getPlayer(); + + if (item == null) return; + if (item.getType() != Material.GOAT_HORN) return; + + Block block = event.getClickedBlock(); + if (player.hasCooldown(Material.GOAT_HORN)) return; + + if (isCustomGoatHorn(event) && ((event.getAction() == Action.RIGHT_CLICK_BLOCK) || (event.getAction() == Action.RIGHT_CLICK_AIR))) { + + if (event.getAction() == Action.RIGHT_CLICK_BLOCK && !player.isSneaking()) { + if ((block.getBlockData() instanceof Powerable) || (block instanceof TileState) && event.useInteractedBlock().equals(Event.Result.ALLOW)) { + return; + } + } + + String soundFileName = event.getItem().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(customDiscs, "customhorn"), PersistentDataType.STRING); + + @NotNull PersistentDataContainer persistentDataContainer = event.getItem().getItemMeta().getPersistentDataContainer(); + + float range; + NamespacedKey customSoundRangeKey = new NamespacedKey(customDiscs, "customsoundrange"); + if(persistentDataContainer.has(customSoundRangeKey, PersistentDataType.FLOAT)) { + range = Math.min(persistentDataContainer.get(customSoundRangeKey, PersistentDataType.FLOAT), CustomDiscs.getInstance().musicDiscMaxDistance); + } else { + range = Math.min(CustomDiscs.getInstance().musicDiscDistance, CustomDiscs.getInstance().musicDiscMaxDistance); + } + + int hornCooldown; + NamespacedKey hornCooldownKey = new NamespacedKey(customDiscs, "customhorncoolodwn"); + if(persistentDataContainer.has(hornCooldownKey, PersistentDataType.INTEGER)) { + hornCooldown = Math.min(persistentDataContainer.get(hornCooldownKey, PersistentDataType.INTEGER), CustomDiscs.getInstance().hornMaxCooldownTicks); + } else { + hornCooldown = Math.min(Math.round(CustomDiscs.getInstance().hornCooldown * 20), CustomDiscs.getInstance().hornMaxCooldownTicks); + } + + Path soundFilePath = Path.of(customDiscs.getDataFolder().getPath(), "musicdata", soundFileName); + + if (soundFilePath.toFile().exists()) { + + Component songNameComponent = Objects.requireNonNull(event.getItem().getItemMeta().lore()).get(0).asComponent(); + String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent); + + TextComponent customActionBarSongPlaying = Component.text() + .content("Now Horn Playing: " + songName) + .color(NamedTextColor.GOLD) + .build(); + + assert VoicePlugin.voicechatServerApi != null; + playerManager.playLocationalAudioHorn(VoicePlugin.voicechatServerApi, soundFilePath, player, customActionBarSongPlaying.asComponent(), range); + player.setCooldown(Material.GOAT_HORN, hornCooldown); + } else { + player.sendMessage(NamedTextColor.RED + "Sound file not found."); + event.setCancelled(true); + throw new FileNotFoundException("Sound file is missing!"); + } + } + } + + public static boolean isCustomGoatHorn(PlayerInteractEvent e) { + if (e.getItem()==null) return false; + return e.getItem().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(customDiscs, "customhorn")); + } + +} \ No newline at end of file diff --git a/src/main/resources/goat_horn_category.png b/src/main/resources/goat_horn_category.png new file mode 100644 index 0000000..e1a7d6d Binary files /dev/null and b/src/main/resources/goat_horn_category.png differ