This commit is contained in:
Minecon724 2024-12-20 08:34:42 +01:00
parent d1a405430e
commit 3053110a1a
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
10 changed files with 67 additions and 20 deletions

View file

@ -3,7 +3,6 @@ package eu.m724.musicPlugin;
import eu.m724.musicPlugin.file.AudioFileStorage; import eu.m724.musicPlugin.file.AudioFileStorage;
import eu.m724.musicPlugin.item.PortableMediaPlayer; import eu.m724.musicPlugin.item.PortableMediaPlayer;
import eu.m724.musicPlugin.item.PortableMediaPlayers; import eu.m724.musicPlugin.item.PortableMediaPlayers;
import eu.m724.musicPlugin.item.Speakers;
import eu.m724.musicPlugin.player.MusicPlayer; import eu.m724.musicPlugin.player.MusicPlayer;
import eu.m724.musicPlugin.file.AudioFile; import eu.m724.musicPlugin.file.AudioFile;
import eu.m724.musicPlugin.player.StaticMusicPlayer; import eu.m724.musicPlugin.player.StaticMusicPlayer;

View file

@ -5,11 +5,14 @@ import de.maxhenkel.voicechat.api.VoicechatConnection;
import de.maxhenkel.voicechat.api.VoicechatServerApi; import de.maxhenkel.voicechat.api.VoicechatServerApi;
import eu.m724.musicPlugin.file.AudioFileStorage; import eu.m724.musicPlugin.file.AudioFileStorage;
import eu.m724.musicPlugin.item.ItemEvents; import eu.m724.musicPlugin.item.ItemEvents;
import eu.m724.musicPlugin.item.speaker.BlockChecker;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public final class MusicPlugin extends JavaPlugin { public final class MusicPlugin extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
Statics.plugin = this;
BukkitVoicechatService service = getServer().getServicesManager().load(BukkitVoicechatService.class); BukkitVoicechatService service = getServer().getServicesManager().load(BukkitVoicechatService.class);
if (service != null) { if (service != null) {
@ -33,6 +36,8 @@ public final class MusicPlugin extends JavaPlugin {
getServer().getPluginManager().registerEvents(new ItemEvents(), this); getServer().getPluginManager().registerEvents(new ItemEvents(), this);
// TODO do this better, maybe along events
new BlockChecker().runTaskTimerAsynchronously(this, 0, 20);
} }
private void onApiStarted(VoicechatServerApi api) { private void onApiStarted(VoicechatServerApi api) {

View file

@ -4,5 +4,6 @@ import de.maxhenkel.voicechat.api.VoicechatServerApi;
// TODO find a better way // TODO find a better way
public class Statics { public class Statics {
public static MusicPlugin plugin;
public static VoicechatServerApi api; public static VoicechatServerApi api;
} }

View file

@ -18,6 +18,8 @@ public class NotEncoder implements OpusEncoder {
i = Math.clamp(frame, 0, size); i = Math.clamp(frame, 0, size);
} }
public int getFrame() { return i; }
public boolean hasRemaining() { public boolean hasRemaining() {
return i < size; return i < size;
} }

View file

@ -3,12 +3,16 @@ package eu.m724.musicPlugin.item;
import eu.m724.musicPlugin.item.speaker.BlockSpeaker; import eu.m724.musicPlugin.item.speaker.BlockSpeaker;
import eu.m724.musicPlugin.item.speaker.Speaker; import eu.m724.musicPlugin.item.speaker.Speaker;
import eu.m724.musicPlugin.player.MovingMusicPlayer; import eu.m724.musicPlugin.player.MovingMusicPlayer;
import org.bukkit.GameMode;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Particle; import org.bukkit.Particle;
import org.bukkit.block.Block;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityDropItemEvent; import org.bukkit.event.entity.EntityDropItemEvent;
import org.bukkit.event.entity.EntityPickupItemEvent; import org.bukkit.event.entity.EntityPickupItemEvent;
@ -32,15 +36,18 @@ public class ItemEvents implements Listener {
if (!event.getPlayer().isSneaking()) { if (!event.getPlayer().isSneaking()) {
if (right) { if (right) {
System.out.println("nos higft + rmb next song"); System.out.println("nos higft + rmb: next song");
// TODO forward song player.next();
} else { } else {
System.out.println("no shift + lmb rpevous song"); System.out.println("no shift + lmb: rpevous song");
// TODO back song player.prev();
} }
} else { } else {
if (right) { if (right) {
System.out.println("shift + rmb bind spekar"); System.out.println("shift + rmb: open inventory");
// TODO do that
} else {
System.out.println("shift + lmb: bind spekar");
var block = event.getClickedBlock(); var block = event.getClickedBlock();
if (block != null) { if (block != null) {
@ -51,11 +58,11 @@ public class ItemEvents implements Listener {
player.setSpeaker(speaker); player.setSpeaker(speaker);
block.getWorld().spawnParticle(Particle.HAPPY_VILLAGER, block.getLocation(), 10); block.getWorld().spawnParticle(Particle.HAPPY_VILLAGER, block.getLocation(), 10);
event.setCancelled(true); // prevent break if (event.getPlayer().getGameMode() == GameMode.CREATIVE)
event.setCancelled(true); // prevent accidental break
} }
} }
} }
// originally inventory was supposed to be here
} }
} }
@ -79,6 +86,7 @@ public class ItemEvents implements Listener {
// TODO do somehting? // TODO do somehting?
} }
@EventHandler
public void onMove(InventoryMoveItemEvent event) { public void onMove(InventoryMoveItemEvent event) {
if (event.getDestination().getType() == InventoryType.PLAYER) { if (event.getDestination().getType() == InventoryType.PLAYER) {
var player = PortableMediaPlayers.get(event.getItem()); var player = PortableMediaPlayers.get(event.getItem());

View file

@ -18,6 +18,10 @@ public class PlayedSong {
return (int) (System.currentTimeMillis() - started) + progress; return (int) (System.currentTimeMillis() - started) + progress;
} }
public void hint(int frame) {
this.progress = frame * 20;
}
public void pause() { public void pause() {
this.progress = getProgress(); this.progress = getProgress();
this.started = -1; this.started = -1;

View file

@ -56,7 +56,8 @@ public class PortableMediaPlayer {
speaker.setDestroyCallback(c -> { speaker.setDestroyCallback(c -> {
System.out.println("spekar rip"); System.out.println("spekar rip");
played.pause(); played.pause(); // this one stops the tracker not playback, which is stopped in Speaker
this.speaker = null;
}); });
this.speaker = speaker; this.speaker = speaker;
@ -76,6 +77,7 @@ public class PortableMediaPlayer {
// track paused // track paused
System.out.println("Okay its paused"); System.out.println("Okay its paused");
played.pause(); played.pause();
played.hint(played.file.getEncoder().getFrame());
} else if (action == MusicPlayer.TrackAction.UNPAUSE) { // track unpaused } else if (action == MusicPlayer.TrackAction.UNPAUSE) { // track unpaused
System.out.println("Okay its unpaused"); System.out.println("Okay its unpaused");
played.unpause(); played.unpause();
@ -115,6 +117,12 @@ public class PortableMediaPlayer {
this.played = null; this.played = null;
} }
void prev() {
System.out.println("pmp prev");
this.played = new PlayedSong(played.file);
unpause();
}
void next() { void next() {
System.out.println("pmp next"); System.out.println("pmp next");
this.played = null; this.played = null;

View file

@ -1,10 +0,0 @@
package eu.m724.musicPlugin.item;
import eu.m724.musicPlugin.item.speaker.Speaker;
import java.util.HashMap;
import java.util.Map;
public class Speakers {
static Map<Integer, Speaker> speakers = new HashMap<>();
}

View file

@ -0,0 +1,12 @@
package eu.m724.musicPlugin.item.speaker;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.HashSet;
public class BlockChecker extends BukkitRunnable {
@Override
public void run() {
new HashSet<>(BlockSpeaker.speakers.values()).forEach(BlockSpeaker::check);
}
}

View file

@ -1,15 +1,22 @@
package eu.m724.musicPlugin.item.speaker; package eu.m724.musicPlugin.item.speaker;
import eu.m724.musicPlugin.Statics;
import eu.m724.musicPlugin.player.MovingMusicPlayer; import eu.m724.musicPlugin.player.MovingMusicPlayer;
import eu.m724.musicPlugin.player.MusicPlayer; import eu.m724.musicPlugin.player.MusicPlayer;
import eu.m724.musicPlugin.player.StaticMusicPlayer; import eu.m724.musicPlugin.player.StaticMusicPlayer;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.metadata.MetadataValueAdapter;
import org.checkerframework.checker.units.qual.N;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class BlockSpeaker extends Speaker { public class BlockSpeaker extends Speaker {
private static final Map<Location, BlockSpeaker> speakers = new HashMap<>(); static final Map<Location, BlockSpeaker> speakers = new HashMap<>();
public static BlockSpeaker get(Location location) { public static BlockSpeaker get(Location location) {
return speakers.get(location); return speakers.get(location);
@ -28,10 +35,21 @@ public class BlockSpeaker extends Speaker {
public BlockSpeaker(Location location) { public BlockSpeaker(Location location) {
super(new StaticMusicPlayer(location)); super(new StaticMusicPlayer(location));
this.location = location; this.location = location;
this.location.getBlock().setMetadata("t_speaker", new FixedMetadataValue(Statics.plugin, true));
} }
@Override @Override
void onDestroy() { void onDestroy() {
speakers.remove(location); speakers.remove(location);
} }
public boolean check() {
if (location.getBlock().getType() == Material.NOTE_BLOCK && location.getBlock().hasMetadata("t_speaker")) {
return true;
} else {
System.out.println("Speaker disaper");
destroy();
return false;
}
}
} }