Skip to content

Commit

Permalink
Fix playing after eject for "Standard" discs
Browse files Browse the repository at this point in the history
  • Loading branch information
sub-kek committed Aug 31, 2024
1 parent 023746f commit 5dbc457
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@
import de.maxhenkel.voicechat.api.VolumeCategory;
import de.maxhenkel.voicechat.api.events.EventRegistration;
import de.maxhenkel.voicechat.api.events.VoicechatServerStartedEvent;
import lombok.Getter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Enumeration;

public class VoicePlugin implements VoicechatPlugin {
public static String MUSIC_DISC_CATEGORY = "music_discs";
@Getter
public class CDVoiceAddon implements VoicechatPlugin {
public static final String MUSIC_DISC_CATEGORY = "music_discs";

public static VoicechatServerApi voicechatApi;
public static VolumeCategory musicDiscs;
private VoicechatServerApi voicechatApi;
private VolumeCategory musicDiscsCategory;

private static CDVoiceAddon instance;
public static CDVoiceAddon getInstance() {
if (instance == null) return instance = new CDVoiceAddon();
return instance;
}

@Override
public String getPluginId() {
Expand All @@ -31,12 +39,12 @@ public void initialize(VoicechatApi api) {
@Override
public void registerEvents(EventRegistration registration) {
registration.registerEvent(VoicechatServerStartedEvent.class, event -> {
musicDiscs = voicechatApi.volumeCategoryBuilder()
musicDiscsCategory = voicechatApi.volumeCategoryBuilder()
.setId(MUSIC_DISC_CATEGORY)
.setName("Music Discs")
.setIcon(getMusicDiscIcon())
.build();
voicechatApi.registerVolumeCategory(musicDiscs);
voicechatApi.registerVolumeCategory(musicDiscsCategory);
});
}

Expand Down
18 changes: 8 additions & 10 deletions src/main/java/io/github/subkek/customdiscs/CustomDiscs.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
import io.github.subkek.customdiscs.event.JukeboxHandler;
import io.github.subkek.customdiscs.language.YamlLanguage;
import io.github.subkek.customdiscs.metrics.BStatsLink;
import io.github.subkek.customdiscs.particle.ParticleManager;
import io.github.subkek.customdiscs.util.Formatter;
import io.github.subkek.customdiscs.util.LegacyUtil;
import lombok.Getter;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.block.Jukebox;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -35,8 +33,7 @@
import java.io.StringWriter;

public class CustomDiscs extends JavaPlugin {
public static final String PLUGIN_ID = "CustomDiscs";
private VoicePlugin voicechatPlugin;
public static final String PLUGIN_ID = "customdiscs";
@Getter
private YamlLanguage language = new YamlLanguage();
@Getter
Expand All @@ -47,6 +44,7 @@ public class CustomDiscs extends JavaPlugin {
@Getter
private BukkitAudiences audience;
public int discsPlayed = 0;
private boolean voicechatAddonRegistered = false;

public static CustomDiscs getPlugin() {
return getPlugin(CustomDiscs.class);
Expand Down Expand Up @@ -92,7 +90,7 @@ public void onPacketSending(PacketEvent event) {
if (LegacyUtil.isCustomDisc(jukebox.getRecord()) ||
LegacyUtil.isCustomYouTubeDisc(jukebox.getRecord())) {
event.setCancelled(true);
ParticleManager.getInstance().start(jukebox);
PhysicsManager.getInstance().start(jukebox);
}
}
}
Expand All @@ -103,8 +101,8 @@ private void registerVoicechatHook() {
BukkitVoicechatService service = getServer().getServicesManager().load(BukkitVoicechatService.class);

if (service != null) {
voicechatPlugin = new VoicePlugin();
service.registerPlugin(voicechatPlugin);
service.registerPlugin(CDVoiceAddon.getInstance());
voicechatAddonRegistered = true;
info("Successfully enabled voicechat hook");
} else {
error("Failed to enable voicechat hook");
Expand All @@ -126,10 +124,10 @@ public void onDisable() {
LavaPlayerManager.getInstance().stopPlayingAll();
LavaPlayerManager.getInstance().save();

PlayerManager.getInstance().stopAll();
PlayerManager.getInstance().stopPlayingAll();

if (voicechatPlugin != null) {
getServer().getServicesManager().unregister(voicechatPlugin);
if (voicechatAddonRegistered) {
getServer().getServicesManager().unregister(CDVoiceAddon.getInstance());
CustomDiscs.info("Successfully disabled CustomDiscs plugin");
}

Expand Down
25 changes: 10 additions & 15 deletions src/main/java/io/github/subkek/customdiscs/LavaPlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import de.maxhenkel.voicechat.api.audiochannel.LocationalAudioChannel;
import dev.lavalink.youtube.YoutubeAudioSourceManager;
import dev.lavalink.youtube.http.YoutubeOauth2Handler;
import io.github.subkek.customdiscs.event.HopperHandler;
import net.kyori.adventure.text.Component;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;

Expand All @@ -28,12 +26,17 @@
import java.util.concurrent.TimeUnit;

public class LavaPlayerManager {
private static LavaPlayerManager instance;
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");

private static LavaPlayerManager instance;
public static LavaPlayerManager getInstance() {
if (instance == null) return instance = new LavaPlayerManager();
return instance;
}

public LavaPlayerManager() {
YoutubeAudioSourceManager source = new YoutubeAudioSourceManager(false);
if (plugin.getCDConfig().isYoutubeOauth2()) {
Expand Down Expand Up @@ -70,13 +73,6 @@ public LavaPlayerManager() {
lavaPlayerManager.registerSourceManager(source);
}

public static LavaPlayerManager getInstance() {
if (Objects.isNull(instance)) {
instance = new LavaPlayerManager();
}
return instance;
}

public void save() {
for (AudioSourceManager manager : lavaPlayerManager.getSourceManagers()) {
if (!(manager instanceof YoutubeAudioSourceManager)) continue;
Expand All @@ -99,16 +95,17 @@ public void save() {
}
}

public void playLocationalAudioYoutube(Block block, VoicechatServerApi api, String ytUrl, Component actionbarComponent) {
public void play(Block block, String ytUrl, Component actionbarComponent) {
UUID uuid = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes());
CustomDiscs.debug("LavaPlayer UUID is {0}", uuid);
if (playerMap.containsKey(uuid)) stopPlaying(uuid);

VoicechatServerApi api = CDVoiceAddon.getInstance().getVoicechatApi();

LavaPlayer lavaPlayer = new LavaPlayer();
playerMap.put(uuid, lavaPlayer);

lavaPlayer.ytUrl = ytUrl;
lavaPlayer.block = block;
lavaPlayer.playerUUID = uuid;
lavaPlayer.audioChannel = api.createLocationalAudioChannel(
UUID.randomUUID(),
Expand All @@ -122,7 +119,7 @@ public void playLocationalAudioYoutube(Block block, VoicechatServerApi api, Str

if (lavaPlayer.audioChannel == null) return;

lavaPlayer.audioChannel.setCategory(VoicePlugin.MUSIC_DISC_CATEGORY);
lavaPlayer.audioChannel.setCategory(CDVoiceAddon.MUSIC_DISC_CATEGORY);
lavaPlayer.audioChannel.setDistance(plugin.getCDConfig().getMusicDiscDistance());

lavaPlayer.playersInRange = api.getPlayersInRange(
Expand Down Expand Up @@ -159,7 +156,6 @@ public void stopPlaying(UUID uuid) {
lavaPlayer.trackFuture.complete(null);
lavaPlayer.audioPlayer.destroy();
lavaPlayer.lavaPlayerThread.interrupt();
HopperHandler.getInstance().discToHopper(lavaPlayer.block);
} else {
CustomDiscs.debug(
"Couldn't find LavaPlayer {0} to stop",
Expand All @@ -184,7 +180,6 @@ private class LavaPlayer {
private UUID playerUUID;
private AudioPlayer audioPlayer;
private final Thread lavaPlayerThread = new Thread(this::startTrackJob, "LavaPlayerThread");
private Block block;

private void startTrackJob() {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package io.github.subkek.customdiscs.particle;
package io.github.subkek.customdiscs;

import com.tcoded.folialib.wrapper.task.WrappedTask;
import io.github.subkek.customdiscs.CustomDiscs;
import io.github.subkek.customdiscs.LavaPlayerManager;
import io.github.subkek.customdiscs.PlayerManager;
import io.github.subkek.customdiscs.util.LegacyUtil;
import lombok.Data;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Jukebox;

import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class ParticleManager {
private static ParticleManager instance;
public class PhysicsManager {
private final CustomDiscs plugin = CustomDiscs.getPlugin();
private final Map<UUID, ParticleJukebox> locationParticleManager = new HashMap<>();
private final Map<UUID, ParticleJukebox> jukeboxMap = new HashMap<>();

public static ParticleManager getInstance() {
if (instance == null) return instance = new ParticleManager();
private static PhysicsManager instance;
public static PhysicsManager getInstance() {
if (instance == null) return instance = new PhysicsManager();
return instance;
}

public ParticleJukebox get(Block block) {
UUID uuid = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes());
ParticleJukebox particleJukebox = locationParticleManager.get(uuid);
ParticleJukebox particleJukebox = jukeboxMap.get(uuid);
if (particleJukebox == null)
throw new IllegalStateException("This ParticleJukebox doesn't exists cannot get");
return particleJukebox;
Expand All @@ -34,41 +34,43 @@ public record NeedUpdate(boolean returnForced, boolean value) {

public NeedUpdate isNeedUpdate(Block block) {
UUID uuid = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes());
ParticleJukebox particleJukebox = locationParticleManager.get(uuid);
ParticleJukebox particleJukebox = jukeboxMap.get(uuid);
if (particleJukebox == null) {
CustomDiscs.debug("ParticleManager return value false because ParticleJukebox is null");
return new NeedUpdate(true, false);
}
return new NeedUpdate(false, particleJukebox.isNeedUpdate());
}

public void setNeedUpdate(Block block, boolean value) {
UUID uuid = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes());
ParticleJukebox particleJukebox = locationParticleManager.get(uuid);
private void discToHopper(Block block) {
if (!plugin.getCDConfig().isAllowHoppers()) return;
if (!plugin.isEnabled()) return;
if (!block.getLocation().getChunk().isLoaded()) return;
if (!block.getType().equals(Material.JUKEBOX)) return;

if (particleJukebox == null)
throw new IllegalStateException("This ParticleJukebox doesn't exists cannot set NeedUpdate value");
Block possibleHopper = block.getRelative(BlockFace.DOWN);
if (!possibleHopper.getType().equals(Material.HOPPER)) return;

particleJukebox.setNeedUpdate(value);
CustomDiscs.debug("Attempting to send a disk to the hopper using a hopper update.");
}

public void stop(Block block) {
private void stop(Block block) {
UUID uuid = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes());
if (locationParticleManager.containsKey(uuid)) {
ParticleJukebox particleJukebox = locationParticleManager.remove(uuid);
if (jukeboxMap.containsKey(uuid)) {
ParticleJukebox particleJukebox = jukeboxMap.remove(uuid);
particleJukebox.task.cancel();

Jukebox jukebox = (Jukebox) block.getState();
jukebox.update();
jukebox.stopPlaying();
discToHopper(block);
}
}

public void start(Jukebox jukebox) {
UUID uuid = UUID.nameUUIDFromBytes(jukebox.getLocation().toString().getBytes());
if (locationParticleManager.containsKey(uuid)) return;
if (jukeboxMap.containsKey(uuid)) return;
ParticleJukebox particleJukebox = new ParticleJukebox();
locationParticleManager.put(uuid, particleJukebox);
jukeboxMap.put(uuid, particleJukebox);

plugin.getFoliaLib().getScheduler().runAtLocationTimer(jukebox.getLocation(), task -> {
particleJukebox.setTask(task);
Expand Down
Loading

0 comments on commit 5dbc457

Please sign in to comment.