Make it better

This commit is contained in:
Minecon724 2025-01-31 14:57:22 +01:00
commit 6c19cfe128
No known key found for this signature in database
GPG key ID: 3CCC4D267742C8E8
9 changed files with 117 additions and 82 deletions

View file

@ -1,44 +0,0 @@
package eu.m724.music_plugin;
import eu.m724.music_plugin.item.PortableMediaPlayer;
import eu.m724.music_plugin.item.PortableMediaPlayers;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.io.IOException;
public class MusicCommands implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equals("pmp")) {
var p = (Player) sender;
var pmp = PortableMediaPlayers.get(p.getItemInHand());
switch (args[0]) {
case "info" -> {
sender.sendMessage(String.valueOf(pmp.id));
sender.sendMessage(String.valueOf(pmp.premium));
sender.sendMessage(pmp.engraving);
}
case "create" -> {
pmp = PortableMediaPlayer.create(args[0].equals("yes"), args[1]);
p.getInventory().addItem(pmp.getItemStack());
}
case "play" -> {
var storage = MusicPlugin.getStorage();
var path = storage.get(args[1], Integer.parseInt(args[2]));
try {
pmp.play(path.toFile());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
return true;
}
}

View file

@ -6,6 +6,7 @@ import eu.m724.music_plugin.audio.storage.AudioFileStorage;
import eu.m724.music_plugin.audio.Converter;
import eu.m724.music_plugin.audio.storage.Downloader;
import eu.m724.music_plugin.item.ItemEvents;
import eu.m724.music_plugin.item.PmpCommand;
import eu.m724.music_plugin.item.speaker.BlockChecker;
import net.bramp.ffmpeg.FFmpeg;
import org.bukkit.NamespacedKey;
@ -59,7 +60,7 @@ public final class MusicPlugin extends JavaPlugin {
.setExecutor(new TestCommand());
Objects.requireNonNull(getCommand("pmp"))
.setExecutor(new MusicCommands());
.setExecutor(new PmpCommand());
getServer().getPluginManager().registerEvents(new ItemEvents(), this);

View file

@ -39,7 +39,7 @@ public class TestCommand implements CommandExecutor {
}
private void testDownload(Player player, String url) {
player.sendMessage("Downloading " + url);
player.sendMessage("Downloading...");
MusicPlugin.getDownloader().download(URI.create(url)).handle((hash, ex) -> {
if (ex != null) {
ex.printStackTrace();
@ -61,6 +61,7 @@ public class TestCommand implements CommandExecutor {
private void testConvert(Player player, String hash, int bitrate) {
try {
player.sendMessage("Converting...");
MusicPlugin.getStorage().convert(MusicPlugin.getConverter(), hash, bitrate).handle((f, ex) -> {
if (ex != null) {
ex.printStackTrace();

View file

@ -34,4 +34,8 @@ public class LocationalChannel extends AbstractChannel<LocationalAudioChannel> {
// we go getChannel() not just channel, because getChannel() creates it if it doesn't exist
getAudioChannel().setDistance(distance);
}
public Location getLocation() {
return this.location;
}
}

View file

@ -43,7 +43,7 @@ public class OpusFilePlayer {
DebugLogger.finer("Changing channel...");
this.channel = channel;
if (!playing) {
if (playing) {
// to not call events pointlessly
var ote = this.onTrackEvent;
this.onTrackEvent = (r) -> {};

View file

@ -0,0 +1,63 @@
package eu.m724.music_plugin.item;
import eu.m724.music_plugin.MusicPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.Arrays;
public class PmpCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
var player = (Player) sender;
var pmp = PortableMediaPlayers.get(player.getInventory().getItemInMainHand());
var action = args.length > 0 ? args[0] : null;
if (action == null) {
if (pmp != null) {
sender.sendMessage("ID: " + pmp.id);
sender.sendMessage("Premium: " + pmp.premium);
var capacityStr = "";
var h = pmp.storageSeconds % 3600;
var m = (pmp.storageSeconds - (h * 3600)) % 60;
var s = pmp.storageSeconds - (h * 3600) - (m * 60);
if (h > 0) {
capacityStr += h + "h ";
}
capacityStr += m + "m " + s + "s ";
sender.sendMessage("Capacity: " + capacityStr);
sender.sendMessage("Bitrate: %d Kbps".formatted(pmp.audioBitrate / 1000));
}
}
if ("create".equals(action)) {
pmp = PortableMediaPlayer.create(args[1].equals("yes"), String.join(" ", Arrays.asList(args).subList(2, args.length)));
player.getInventory().addItem(pmp.getItemStack());
} else if ("play".equals(action)) {
if (pmp != null) {
var storage = MusicPlugin.getStorage();
var path = storage.get(args[1], Integer.parseInt(args[2]));
try {
pmp.play(path.toFile());
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
player.sendMessage("You must hold a Portable Music Player");
}
} else {
player.sendMessage("create | play");
}
return true;
}
}

View file

@ -13,6 +13,7 @@ import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
@ -22,13 +23,13 @@ import java.util.concurrent.ThreadLocalRandom;
public class PortableMediaPlayer {
static final NamespacedKey idKey = MusicPlugin.getNamespacedKey("player_id");
static final NamespacedKey dataKey = MusicPlugin.getNamespacedKey("player_data");;
static final NamespacedKey dataKey = MusicPlugin.getNamespacedKey("player_data");
public final int id;
// TODO configurable
private final int storageSeconds;
private final int audioBitrate;
public final int storageSeconds;
public final int audioBitrate;
public final boolean premium;
public final String engraving;
@ -66,7 +67,8 @@ public class PortableMediaPlayer {
});
this.speaker = speaker;
player.setChannel(speaker.getChannel().getAudioChannel());
if (player != null)
player.setChannel(speaker.getChannel().getAudioChannel());
}
public void play(File file) throws IOException {
@ -130,16 +132,28 @@ public class PortableMediaPlayer {
/* Item functions */
public ItemStack getItemStack() {
var is = new ItemStack(Material.IRON_INGOT);
var is = new ItemStack(premium ? Material.GOLD_INGOT : Material.IRON_INGOT);
var meta = is.getItemMeta();
meta.setItemName("Portable Music Player");
if (premium) {
var hue = (id & 0xFFFFFF) / (float) 0xFFFFFF;
var saturation = (id >> 24) & 0xF;
var color = Color.getHSBColor(hue, 0.84f + saturation / 15.0f, 1.0f);
meta.setItemName(ChatColor.of(color) + "Portable Music Player");
if (engraving != null)
meta.setLore(List.of(ChatColor.of(color.darker()) + engraving));
} else {
meta.setItemName("Portable Music Player");
if (engraving != null) // custom colors only in premium
meta.setLore(List.of(ChatColor.GRAY + ChatColor.stripColor(engraving)));
}
meta.addEnchant(Enchantment.UNBREAKING, 1, false);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
meta.getPersistentDataContainer().set(idKey, PersistentDataType.INTEGER, id);
meta.getPersistentDataContainer().set(dataKey, PersistentDataType.BYTE_ARRAY, getData());
if (engraving != null)
meta.setLore(List.of(premium ? (ChatColor.GOLD + "" + ChatColor.BOLD + engraving) : (ChatColor.GRAY + ChatColor.stripColor(engraving))));
is.setItemMeta(meta);

View file

@ -1,5 +1,7 @@
package eu.m724.music_plugin.library;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@ -15,7 +17,18 @@ public class Library {
tracks.remove(track);
}
public Track getPlayingTrack() {
public Track getPlaying() {
return tracks.get(playingTrack);
}
public static Library load(InputStream inputStream) throws IOException {
var tracks = new ArrayList<Track>();
byte[] bytes = new byte[32];
while (inputStream.read(bytes) == 32) {
tracks.add(new Track(bytes));
}
return new Library()
}
}

View file

@ -1,37 +1,20 @@
package eu.m724.music_plugin.library;
import java.io.File;
import java.util.HexFormat;
public class Track {
public final File file;
private final byte[] hash;
private int progress = 0;
private long started = -1;
public Track(File file) {
this.file = file;
public Track(byte[] hash) {
assert hash.length == 32;
this.hash = hash;
}
public int getProgress() {
if (started == -1)
return progress;
return (int) (System.currentTimeMillis() - started) + progress;
public Track(String hash) {
this(HexFormat.of().parseHex(hash));
}
public void hint(int frame) {
this.progress = frame * 20;
}
public void pause() {
this.progress = getProgress();
this.started = -1;
}
public void unpause() {
this.started = System.currentTimeMillis();
}
public boolean paused() {
return started == -1;
public byte[] getHash() {
return hash;
}
}