Some work
This commit is contained in:
parent
51c9b203ac
commit
a4cdc28b57
9 changed files with 185 additions and 4 deletions
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
6
src/main/java/eu/m724/musicPlugin/item/ItemEvents.java
Normal file
6
src/main/java/eu/m724/musicPlugin/item/ItemEvents.java
Normal file
|
@ -0,0 +1,6 @@
|
|||
package eu.m724.musicPlugin.item;
|
||||
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class ItemEvents implements Listener {
|
||||
}
|
70
src/main/java/eu/m724/musicPlugin/item/ItemStorage.java
Normal file
70
src/main/java/eu/m724/musicPlugin/item/ItemStorage.java
Normal file
|
@ -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<Integer, PortableMediaPlayer> 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();
|
||||
}
|
||||
}
|
|
@ -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<PortableMediaPlayer> 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
8
src/main/java/eu/m724/musicPlugin/item/SongLibrary.java
Normal file
8
src/main/java/eu/m724/musicPlugin/item/SongLibrary.java
Normal file
|
@ -0,0 +1,8 @@
|
|||
package eu.m724.musicPlugin.item;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SongLibrary {
|
||||
private List<String> library;
|
||||
private int playingIndex = 0;
|
||||
}
|
|
@ -19,7 +19,7 @@ public class EntityMusicPlayer extends MusicPlayer {
|
|||
api.fromEntity(entity)
|
||||
);
|
||||
|
||||
channel.setCategory("music724");
|
||||
channel.setCategory("musicc");
|
||||
channel.setDistance(32);
|
||||
|
||||
return channel;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -27,4 +27,5 @@ commands:
|
|||
usage: /<command>
|
||||
resume:
|
||||
description: Resumes playback
|
||||
usage: /<command>
|
||||
usage: /<command>
|
||||
pmp:
|
Loading…
Reference in a new issue