From a4cdc28b5729b0e6e8245b66dd3f7e2e4d7e5159 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Mon, 16 Dec 2024 20:23:16 +0100 Subject: [PATCH] Some work --- .../eu/m724/musicPlugin/MusicCommands.java | 12 +++ .../java/eu/m724/musicPlugin/MusicPlugin.java | 7 +- .../eu/m724/musicPlugin/item/ItemEvents.java | 6 ++ .../eu/m724/musicPlugin/item/ItemStorage.java | 70 ++++++++++++++++ .../musicPlugin/item/PortableMediaPlayer.java | 79 +++++++++++++++++++ .../eu/m724/musicPlugin/item/SongLibrary.java | 8 ++ .../musicPlugin/player/EntityMusicPlayer.java | 2 +- .../musicPlugin/player/StaticMusicPlayer.java | 2 +- src/main/resources/plugin.yml | 3 +- 9 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 src/main/java/eu/m724/musicPlugin/item/ItemEvents.java create mode 100644 src/main/java/eu/m724/musicPlugin/item/ItemStorage.java create mode 100644 src/main/java/eu/m724/musicPlugin/item/PortableMediaPlayer.java create mode 100644 src/main/java/eu/m724/musicPlugin/item/SongLibrary.java diff --git a/src/main/java/eu/m724/musicPlugin/MusicCommands.java b/src/main/java/eu/m724/musicPlugin/MusicCommands.java index ccf20eb..07c05c5 100644 --- a/src/main/java/eu/m724/musicPlugin/MusicCommands.java +++ b/src/main/java/eu/m724/musicPlugin/MusicCommands.java @@ -1,6 +1,7 @@ package eu.m724.musicPlugin; import eu.m724.musicPlugin.file.AudioFileStorage; +import eu.m724.musicPlugin.item.PortableMediaPlayer; import eu.m724.musicPlugin.player.MusicPlayer; import eu.m724.musicPlugin.file.AudioFile; import eu.m724.musicPlugin.player.StaticMusicPlayer; @@ -101,6 +102,17 @@ public class MusicCommands implements CommandExecutor { } else if (command.getName().equals("resume")) { player.unpause(); sender.sendMessage("unapuised"); + } else if (command.getName().equals("pmp")) { + var p = (Player) sender; + var pmp = PortableMediaPlayer.fromItemStack(p.getItemInHand()).join(); + if (pmp != null) { + sender.sendMessage(String.valueOf(pmp.id)); + sender.sendMessage(String.valueOf(pmp.premium)); + sender.sendMessage(pmp.engraving); + } else { + pmp = PortableMediaPlayer.create(args[0].equals("yes"), args[1]); + p.getInventory().addItem(pmp.getItemStack()); + } } return true; diff --git a/src/main/java/eu/m724/musicPlugin/MusicPlugin.java b/src/main/java/eu/m724/musicPlugin/MusicPlugin.java index a1115eb..ed73d96 100644 --- a/src/main/java/eu/m724/musicPlugin/MusicPlugin.java +++ b/src/main/java/eu/m724/musicPlugin/MusicPlugin.java @@ -4,6 +4,8 @@ import de.maxhenkel.voicechat.api.BukkitVoicechatService; import de.maxhenkel.voicechat.api.VoicechatConnection; import de.maxhenkel.voicechat.api.VoicechatServerApi; import eu.m724.musicPlugin.file.AudioFileStorage; +import eu.m724.musicPlugin.item.ItemStorage; +import org.bukkit.entity.Item; import org.bukkit.plugin.java.JavaPlugin; public final class MusicPlugin extends JavaPlugin { @@ -28,6 +30,9 @@ public final class MusicPlugin extends JavaPlugin { getCommand("stop").setExecutor(mcmd); getCommand("pause").setExecutor(mcmd); getCommand("resume").setExecutor(mcmd); + getCommand("pmp").setExecutor(mcmd); + + ItemStorage.init(this); } @@ -35,7 +40,7 @@ public final class MusicPlugin extends JavaPlugin { getLogger().info("registerating..."); var category = api.volumeCategoryBuilder() - .setId("music724") + .setId("musicc") .setName("Music players") .build(); diff --git a/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java b/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java new file mode 100644 index 0000000..ae62ea9 --- /dev/null +++ b/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java @@ -0,0 +1,6 @@ +package eu.m724.musicPlugin.item; + +import org.bukkit.event.Listener; + +public class ItemEvents implements Listener { +} diff --git a/src/main/java/eu/m724/musicPlugin/item/ItemStorage.java b/src/main/java/eu/m724/musicPlugin/item/ItemStorage.java new file mode 100644 index 0000000..1f5abcb --- /dev/null +++ b/src/main/java/eu/m724/musicPlugin/item/ItemStorage.java @@ -0,0 +1,70 @@ +package eu.m724.musicPlugin.item; + +import eu.m724.musicPlugin.MusicPlugin; +import org.bukkit.inventory.ItemStack; +import org.bukkit.persistence.PersistentDataType; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.HashMap; +import java.util.HexFormat; +import java.util.Map; + +public class ItemStorage { + private static ItemStorage INSTANCE; + + private File directory; + private Map cache = new HashMap<>(); // TODO clean cache + + public static void init(MusicPlugin plugin) { + INSTANCE = new ItemStorage(); + INSTANCE.directory = new File(plugin.getDataFolder(), "storage"); + } + + static ItemStorage getInstance() { + return INSTANCE; + } + + public PortableMediaPlayer getPortableMediaPlayer(int id) throws IOException { + var file = new File(directory, HexFormat.of().toHexDigits(id)); + + if (!file.exists()) return null; + + ByteBuffer buffer = ByteBuffer.wrap(Files.readAllBytes(file.toPath())); + + if (buffer.get() != 0) { // TODO version mismatch + + } + + var premium = buffer.get() == 1; + + var eb = new byte[Byte.toUnsignedInt(buffer.get())]; + buffer.get(eb); + var engraving = new String(eb, StandardCharsets.UTF_8); + + return new PortableMediaPlayer(id, premium, engraving); + } + + public void savePortableMediaPlayer(PortableMediaPlayer player) throws IOException { + var file = new File(directory, HexFormat.of().toHexDigits(player.id)); + + Files.write(file.toPath(), getData(player)); + } + + + public byte[] getData(PortableMediaPlayer player) { + var buffer = ByteBuffer.allocate(8 + player.engraving.length()); + buffer.put((byte) 0); // version + buffer.put((byte) (player.premium ? 1 : 0)); + + buffer.put((byte) player.engraving.length()); + buffer.put(player.engraving.getBytes(StandardCharsets.UTF_8)); + + return buffer.array(); + } +} diff --git a/src/main/java/eu/m724/musicPlugin/item/PortableMediaPlayer.java b/src/main/java/eu/m724/musicPlugin/item/PortableMediaPlayer.java new file mode 100644 index 0000000..d31823a --- /dev/null +++ b/src/main/java/eu/m724/musicPlugin/item/PortableMediaPlayer.java @@ -0,0 +1,79 @@ +package eu.m724.musicPlugin.item; + +import net.md_5.bungee.api.ChatColor; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.persistence.PersistentDataType; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.ThreadLocalRandom; + +public class PortableMediaPlayer { + private static NamespacedKey namespacedKey = new NamespacedKey("tweaks724", "player"); + + public final int id; + + // TODO configurable + public final int storageSeconds = 600; + public final int audioBitrate = 32000; + + public final boolean premium; + public final String engraving; + + SongLibrary library; + + public PortableMediaPlayer(int id, boolean premium, String engraving) { + this.id = id; + this.premium = premium; + this.engraving = engraving; + } + + public static PortableMediaPlayer create(boolean premium, String engraving) { + var pmp = new PortableMediaPlayer(ThreadLocalRandom.current().nextInt(), premium, engraving); + CompletableFuture.runAsync(() -> { + try { + ItemStorage.getInstance().savePortableMediaPlayer(pmp); + } catch (IOException ex) { + ex.printStackTrace(); + } + }); + return pmp; + } + + public ItemStack getItemStack() { + var is = new ItemStack(Material.IRON_INGOT); + var meta = is.getItemMeta(); + + meta.addEnchant(Enchantment.UNBREAKING, 1, false); + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + meta.getPersistentDataContainer().set(namespacedKey, PersistentDataType.INTEGER, id); + if (engraving != null) + meta.setLore(List.of(premium ? (ChatColor.GOLD + "" + ChatColor.BOLD + engraving) : (ChatColor.GRAY + ChatColor.stripColor(engraving)))); + + is.setItemMeta(meta); + + return is; + } + + public static CompletableFuture fromItemStack(ItemStack itemStack) { + return CompletableFuture.supplyAsync(() -> { + var meta = itemStack.getItemMeta(); + if (meta == null) return null; + + var id = meta.getPersistentDataContainer().get(namespacedKey, PersistentDataType.INTEGER); + if (id == null) return null; + + try { + return ItemStorage.getInstance().getPortableMediaPlayer(id); + } catch (IOException e) { + throw new CompletionException(e); + } + }); + } +} diff --git a/src/main/java/eu/m724/musicPlugin/item/SongLibrary.java b/src/main/java/eu/m724/musicPlugin/item/SongLibrary.java new file mode 100644 index 0000000..615db6e --- /dev/null +++ b/src/main/java/eu/m724/musicPlugin/item/SongLibrary.java @@ -0,0 +1,8 @@ +package eu.m724.musicPlugin.item; + +import java.util.List; + +public class SongLibrary { + private List library; + private int playingIndex = 0; +} diff --git a/src/main/java/eu/m724/musicPlugin/player/EntityMusicPlayer.java b/src/main/java/eu/m724/musicPlugin/player/EntityMusicPlayer.java index fe1edc5..c32ac6d 100644 --- a/src/main/java/eu/m724/musicPlugin/player/EntityMusicPlayer.java +++ b/src/main/java/eu/m724/musicPlugin/player/EntityMusicPlayer.java @@ -19,7 +19,7 @@ public class EntityMusicPlayer extends MusicPlayer { api.fromEntity(entity) ); - channel.setCategory("music724"); + channel.setCategory("musicc"); channel.setDistance(32); return channel; diff --git a/src/main/java/eu/m724/musicPlugin/player/StaticMusicPlayer.java b/src/main/java/eu/m724/musicPlugin/player/StaticMusicPlayer.java index 1ea789d..3f4aec1 100644 --- a/src/main/java/eu/m724/musicPlugin/player/StaticMusicPlayer.java +++ b/src/main/java/eu/m724/musicPlugin/player/StaticMusicPlayer.java @@ -20,7 +20,7 @@ public class StaticMusicPlayer extends MusicPlayer { api.createPosition(location.getX(), location.getY(), location.getZ()) ); - channel.setCategory("music724"); + channel.setCategory("musicc"); channel.setDistance(32); return channel; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index a46c688..0125e2b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -27,4 +27,5 @@ commands: usage: / resume: description: Resumes playback - usage: / \ No newline at end of file + usage: / + pmp: \ No newline at end of file