Vanilla Behavior

Custom discs now emulate vanilla behavior.
This commit is contained in:
Navoei
2024-11-27 23:16:47 -06:00
parent 70bfe36821
commit fd3e0db94c
7 changed files with 47 additions and 63 deletions

View File

@@ -12,6 +12,6 @@ mod_id=customdiscsplugin
voicechat_api_version=2.5.0
command_api_version=9.6.0
plugin_version=3.3.0a
plugin_version=4.0
maven_group=me.Navoei.customdiscsplugin
archives_base_name=custom-discs

View File

@@ -12,12 +12,11 @@ import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPIBukkitConfig;
import me.Navoei.customdiscsplugin.command.CustomDiscCommand;
import me.Navoei.customdiscsplugin.event.JukeBox;
import me.Navoei.customdiscsplugin.event.HornPlay;
import me.Navoei.customdiscsplugin.language.Lang;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Jukebox;
import org.bukkit.entity.Player;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
@@ -74,15 +73,14 @@ public final class CustomDiscs extends JavaPlugin {
}
getServer().getPluginManager().registerEvents(new JukeBox(), this);
getServer().getPluginManager().registerEvents(new HornPlay(), this);
getServer().getPluginManager().registerEvents(new HopperManager(), this);
musicDiscDistance = Objects.requireNonNull(getConfig().getInt("music-disc-distance"));
musicDiscMaxDistance = Objects.requireNonNull(getConfig().getInt("music-disc-max-distance"));
musicDiscMaxDistance = Objects.requireNonNull(getConfig().getInt("music-disc-max-distance"));
musicDiscVolume = Float.parseFloat(Objects.requireNonNull(getConfig().getString("music-disc-volume")));
hornCooldown = Float.parseFloat(Objects.requireNonNull(getConfig().getString("horn-cooldown")));
hornMaxCooldown = Objects.requireNonNull(getConfig().getInt("horn-max-cooldown"));
hornMaxCooldownTicks = (Objects.requireNonNull(getConfig().getInt("horn-max-cooldown")) * 20);
hornMaxCooldown = Objects.requireNonNull(getConfig().getInt("horn-max-cooldown"));
hornMaxCooldownTicks = (Objects.requireNonNull(getConfig().getInt("horn-max-cooldown")) * 20);
ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
@@ -97,12 +95,13 @@ public final class CustomDiscs extends JavaPlugin {
if (!jukebox.getRecord().hasItemMeta()) return;
if (jukebox.getRecord().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(this.plugin, "customdisc"), PersistentDataType.STRING)) {
jukebox.stopPlaying();
event.setCancelled(true);
}
//Spawn particles if there isnt any music playing at this location.
ParticleManager.start(jukebox);
//Start the jukebox state manager.
//This keeps the jukebox powered while custom song is playing,
//which perfectly emulated the vanilla behavior of discs.
JukeboxStateManager.start(jukebox);
}
}
});
@@ -122,14 +121,6 @@ public final class CustomDiscs extends JavaPlugin {
return instance;
}
public static boolean isMusicDisc(Player p) {
return p.getInventory().getItemInMainHand().getType().toString().contains("MUSIC_DISC");
}
public static boolean isGoatHorn(Player p) {
return p.getInventory().getItemInMainHand().getType().toString().contains("GOAT_HORN");
}
/**
* Load the lang.yml file.
*
@@ -200,4 +191,12 @@ public final class CustomDiscs extends JavaPlugin {
}
}
public static boolean isMusicDisc(Player p) {
return p.getInventory().getItemInMainHand().getType().toString().contains("MUSIC_DISC");
}
public static boolean isGoatHorn(Player p) {
return p.getInventory().getItemInMainHand().getType().toString().contains("GOAT_HORN");
}
}

View File

@@ -75,42 +75,23 @@ public class HopperManager implements Listener {
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onJukeboxEjectToHopper(InventoryMoveItemEvent event) {
public void onJukeboxEjectToHopperMinecart(InventoryMoveItemEvent event) {
//logger.warning("Enter : onJukeboxEjectToHopper");
InventoryHolder holderSource = event.getSource().getHolder();
InventoryHolder holderDestination = event.getDestination().getHolder();
if (event.getSource().getLocation() == null) return;
if (!event.getSource().getType().equals(InventoryType.JUKEBOX)) return;
if (event.getItem().getItemMeta() == null) return;
if (!isCustomMusicDisc(event.getItem())) return;
if (holderDestination instanceof HopperMinecart) {
discToHopper(((BlockState) holderSource).getBlock());
stopDiscOnEject(((BlockState) holderSource).getBlock());
} else {
event.setCancelled(playerManager.isAudioPlayerPlaying(event.getSource().getLocation()));
stopDisc(((BlockState) holderSource).getBlock());
}
}
public void discToHopper(Block block) {
//logger.warning("Enter : discToHopper");
if (block == null) return;
if (!block.getLocation().getChunk().isLoaded()) return;
if (!block.getType().equals(Material.JUKEBOX)) return;
Jukebox jukebox = (Jukebox) block.getState();
if (jukebox.isPlaying()) {
jukebox.stopPlaying();
}
//Set the block type to force an update.
block.setType(Material.JUKEBOX);
jukebox.update(true, true);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onChunkLoad(ChunkLoadEvent event) {
//logger.warning("Enter : onChunkLoad");
@@ -119,19 +100,27 @@ public class HopperManager implements Listener {
if (!jukebox.hasRecord()) return;
if (!PlayerManager.instance().isAudioPlayerPlaying(blockState.getLocation()) && isCustomMusicDisc(jukebox.getRecord())) {
//Set the block type to force an update.
blockState.getBlock().setType(Material.JUKEBOX);
jukebox.update(true, true);
jukebox.stopPlaying();
}
}
}
}
public void discToHopper(Block block) {
if (block == null) return;
if (!block.getLocation().getChunk().isLoaded()) return;
if (!block.getType().equals(Material.JUKEBOX)) return;
Jukebox jukebox = (Jukebox) block.getState();
jukebox.stopPlaying();
}
private boolean isCustomMusicDisc(ItemStack item) {
//logger.warning("Enter : isCustomMusicDisc");
return item.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(customDiscs, "customdisc"), PersistentDataType.STRING);
}
private void stopDiscOnEject(Block block) {
private void stopDisc(Block block) {
playerManager.stopLocationalAudio(block.getLocation());
}

View File

@@ -1,23 +1,22 @@
package me.Navoei.customdiscsplugin;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.block.Jukebox;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.HashMap;
public class ParticleManager extends BukkitRunnable {
public class JukeboxStateManager extends BukkitRunnable {
static PlayerManager playerManager = PlayerManager.instance();
static HashMap<Location, ParticleManager> locationParticleManager = new HashMap<>();
static HashMap<Location, JukeboxStateManager> locationParticleManager = new HashMap<>();
public static void start(Jukebox jukebox) {
ParticleManager particleManager = new ParticleManager();
particleManager.jukebox = jukebox;
JukeboxStateManager jukeboxStateManager = new JukeboxStateManager();
jukeboxStateManager.jukebox = jukebox;
if (locationParticleManager.containsKey(jukebox.getLocation())) return;
locationParticleManager.put(jukebox.getLocation(), particleManager);
locationParticleManager.get(jukebox.getLocation()).runTaskTimer(CustomDiscs.getInstance(), 0, 20);
locationParticleManager.put(jukebox.getLocation(), jukeboxStateManager);
locationParticleManager.get(jukebox.getLocation()).runTaskTimer(CustomDiscs.getInstance(), 0, 1);
}
//private float seconds;
@@ -28,12 +27,13 @@ public class ParticleManager extends BukkitRunnable {
public void run() {
if (!playerManager.isAudioPlayerPlaying(jukebox.getLocation())) {
jukebox.stopPlaying();
locationParticleManager.remove(jukebox.getLocation());
cancel();
} else {
//if (!jukebox.isPlaying()) {
jukebox.getLocation().getWorld().spawnParticle(Particle.NOTE, jukebox.getLocation().add(0.5, 1.1, 0.5), 1);
//}
if (!jukebox.isPlaying()) {
jukebox.startPlaying();
}
}
}

View File

@@ -85,7 +85,7 @@ public class PlayerManager {
audioPlayer.setOnStopped(() -> {
//Stuff that runs once the audio player ends.
//Stop the vanilla disc if it is playing. This ensures the hopper will pickup the disc.
//Stop the disc and let it flow into the hopper.
Bukkit.getScheduler().runTask(CustomDiscs.getInstance(), () -> HopperManager.instance().discToHopper(block));
playerMap.remove(id);
@@ -101,6 +101,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());
@@ -155,6 +156,7 @@ public class PlayerManager {
}
});
}
*/
@Nullable
private de.maxhenkel.voicechat.api.audiochannel.AudioPlayer playChannel(VoicechatServerApi api, AudioChannel audioChannel, Block block, Path soundFilePath, Collection<ServerPlayer> playersInRange) {
@@ -174,6 +176,7 @@ public class PlayerManager {
}
}
/*
@Nullable
private de.maxhenkel.voicechat.api.audiochannel.AudioPlayer playChannelHorn(VoicechatServerApi api, AudioChannel audioChannel, Player block, Path soundFilePath, Collection<ServerPlayer> playersInRange) {
try {
@@ -191,6 +194,7 @@ public class PlayerManager {
return null;
}
}
*/
private static short[] readSoundFile(Path file) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
return VoicePlugin.voicechatApi.getAudioConverter().bytesToShorts(convertFormat(file, FORMAT));

View File

@@ -1,5 +0,0 @@
package me.Navoei.customdiscsplugin;
public class YoutubePlayerManager {
//Work in progress.
}

View File

@@ -1,12 +1,10 @@
package me.Navoei.customdiscsplugin.event;
import me.Navoei.customdiscsplugin.CustomDiscs;
import me.Navoei.customdiscsplugin.ParticleManager;
import me.Navoei.customdiscsplugin.PlayerManager;
import me.Navoei.customdiscsplugin.VoicePlugin;
import me.Navoei.customdiscsplugin.language.Lang;
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.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
@@ -72,7 +70,6 @@ public class JukeBox implements Listener{
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);
Component customActionBarSongPlaying = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.NOW_PLAYING.toString().replace("%song_name%", songName));