Actionbar & bugfixes

Fixed some bugs while playing customdiscs while vanilla discs were already in a jukebox.
Players are now sent an action bar message containing the song that is being played.
This commit is contained in:
Navoei
2022-07-12 00:57:22 -05:00
parent 53f1396738
commit 2e3d7c4bb3

View File

@@ -1,15 +1,16 @@
package me.Navoei.customdiscsplugin.event;
import de.maxhenkel.voicechat.api.audiochannel.AudioChannel;
import de.maxhenkel.voicechat.api.audiochannel.AudioPlayer;
import de.maxhenkel.voicechat.api.audiochannel.LocationalAudioChannel;
import me.Navoei.customdiscsplugin.CustomDiscs;
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.format.TextDecoration;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Jukebox;
import org.bukkit.block.TileState;
import org.bukkit.entity.Player;
@@ -50,14 +51,20 @@ public class JukeBox implements Listener {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getClickedBlock() == null || event.getItem() == null || event.getItem().getItemMeta() == null || block == null) return;
if (event.getClickedBlock().getType() != Material.JUKEBOX) return;
if (isCustomMusicDisc(event) && !jukeboxContainsPersistentData(block)) {
TileState tileState = (TileState) block.getState();
PersistentDataContainer container = tileState.getPersistentDataContainer();
NamespacedKey key = new NamespacedKey(CustomDiscs.getInstance(),
"CustomDisc");
if (container.has(key, PersistentDataType.BYTE_ARRAY)) return;
TileState tileState = (TileState) block.getState();
PersistentDataContainer container = tileState.getPersistentDataContainer();
NamespacedKey key = new NamespacedKey(CustomDiscs.getInstance(),
"CustomDisc");
if (container.has(key, PersistentDataType.BYTE_ARRAY)) {
event.setCancelled(true);
ejectDisc(block);
return;
}
if (isCustomMusicDisc(event) && !jukeboxContainsPersistentData(block) && !jukeboxContainsVanillaDisc(block)) {
event.setCancelled(true);
container.set(key, PersistentDataType.BYTE_ARRAY, event.getItem().serializeAsBytes());
tileState.update();
@@ -70,8 +77,8 @@ public class JukeBox implements Listener {
if (soundFilePath.toFile().exists()) {
//Component songNameComponent = Objects.requireNonNull(event.getItem().getItemMeta().lore()).get(0).asComponent();
//String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent);
Component songNameComponent = Objects.requireNonNull(event.getItem().getItemMeta().lore()).get(0).asComponent();
String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent);
LocationalAudioChannel audioChannel = VoicePlugin.voicechatServerApi.createLocationalAudioChannel(id, VoicePlugin.voicechatApi.fromServerLevel(block.getLocation().getWorld()), VoicePlugin.voicechatApi.createPosition(block.getLocation().getX() + 0.5d, block.getLocation().getY() + 0.5d, block.getLocation().getZ() + 0.5d));
@@ -80,6 +87,15 @@ public class JukeBox implements Listener {
playerMap.put(id, audioPlayer);
audioPlayer.startPlaying();
player.getInventory().setItem(Objects.requireNonNull(event.getHand()), null);
//Send Player Action Bar
TextComponent customLoreSong = Component.text()
.content("Now Playing: " + songName)
.color(NamedTextColor.GOLD)
.build();
player.sendActionBar(customLoreSong.asComponent());
} catch (Exception e) {
player.sendMessage(ChatColor.RED + "An error occurred while trying to play the music!");
}
@@ -194,6 +210,11 @@ public class JukeBox implements Listener {
return container.has(key, PersistentDataType.BYTE_ARRAY);
}
private boolean jukeboxContainsVanillaDisc(Block b) {
Jukebox jukebox = (Jukebox) b.getLocation().getBlock().getState();
return jukebox.getPlaying() != Material.AIR;
}
public static short[] readSoundFile(Path file) throws UnsupportedAudioFileException, IOException {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(file.toFile());
AudioInputStream convertedInputStream = AudioSystem.getAudioInputStream(FORMAT, inputStream);
@@ -227,10 +248,33 @@ public class JukeBox implements Listener {
return false;
}
//Might Replace some code with this:
private void ejectDisc(Block block) {
UUID id = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes());
//Spawn in item at the block position
TileState tileState = (TileState) block.getState();
PersistentDataContainer container = tileState.getPersistentDataContainer();
NamespacedKey key = new NamespacedKey(CustomDiscs.getInstance(),
"CustomDisc");
if (!container.has(key, PersistentDataType.BYTE_ARRAY)) return;
container.get(key, PersistentDataType.BYTE_ARRAY);
block.getWorld().dropItemNaturally(block.getLocation(), ItemStack.deserializeBytes(container.get(key, PersistentDataType.BYTE_ARRAY)));
container.remove(key);
tileState.update();
if (isAudioPlayerPlaying(playerMap, id)) {
stopAudioPlayer(playerMap, id);
}
}
private void stopAudioPlayer(Map<UUID, AudioPlayer> playerMap, UUID id) {
AudioPlayer audioPlayer = playerMap.get(id);
if (audioPlayer != null && audioPlayer.isPlaying()) {
audioPlayer.stopPlaying();
}
}
}