Files
CustomDiscItem.java
CustomMusicDiscs.java
DiscDropManager.java
MobDeathListener.java
MusicDiscListener.java
MusicDiscManager.java
plugin.yml
Code
7 686
CustomMusicDiscs.java
package com.custommusicdiscs;
import java.io.File;
import java.io.IOException;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
public class CustomMusicDiscs extends JavaPlugin {
private MusicDiscManager discManager;
private DiscDropManager dropManager;
private CustomDiscItem customDiscItem;
// settings.yml driven values
private double soundRange;
private float soundVolume;
private double minDiscDistance;
public void onEnable() {
File musicFolder = new File(this.getDataFolder(), "music");
if (!musicFolder.exists()) {
musicFolder.mkdirs();
}
this.loadSettings();
this.customDiscItem = new CustomDiscItem(this);
this.discManager = new MusicDiscManager(this, this.soundRange, this.soundVolume, this.minDiscDistance);
this.dropManager = new DiscDropManager(this);
this.getServer().getPluginManager().registerEvents((Listener) new MusicDiscListener(this, this.discManager, this.customDiscItem), (Plugin) this);
this.getServer().getPluginManager().registerEvents((Listener) new MobDeathListener(this, this.dropManager, this.customDiscItem), (Plugin) this);
this.getLogger().info("CustomMusicDiscs plugin enabled!");
this.getLogger().info("Place .ogg files in: " + musicFolder.getAbsolutePath());
this.getLogger().info("Mob drops: " + (this.dropManager.isEnabled() ? "ENABLED" : "DISABLED"));
this.getLogger().info("Sound range: " + this.soundRange + " blocks, min distance between discs: " + this.minDiscDistance + " blocks");
}
private void loadSettings() {
File settingsFile = new File(this.getDataFolder(), "settings.yml");
if (!settingsFile.exists()) {
FileConfiguration defaults = new YamlConfiguration();
// Vanilla jukeboxes broadcast at volume 4.0, audible roughly 64 blocks away.
defaults.set("sound-range", 64.0);
defaults.set("sound-volume", 4.0);
// Minimum distance required from another already-playing custom disc
// before a new one is allowed to start, to avoid overlapping/glitchy audio.
defaults.set("min-disc-distance", 32.0);
try {
defaults.save(settingsFile);
this.getLogger().info("Created default settings.yml configuration");
} catch (IOException e) {
this.getLogger().severe("Could not create settings.yml: " + e.getMessage());
}
}
FileConfiguration config = YamlConfiguration.loadConfiguration(settingsFile);
this.soundRange = config.getDouble("sound-range", 64.0);
this.soundVolume = (float) config.getDouble("sound-volume", 4.0);
this.minDiscDistance = config.getDouble("min-disc-distance", 32.0);
}
/**
* Re-reads settings.yml from disk and pushes the new values into the
* already-running MusicDiscManager. Called by /reloadsettings.
*/
public void reloadSettings() {
this.loadSettings();
if (this.discManager != null) {
this.discManager.updateSettings(this.soundRange, this.soundVolume, this.minDiscDistance);
}
}
public void onDisable() {
this.getLogger().info("CustomMusicDiscs plugin disabled!");
}
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("loadmusic")) {
if (!(sender instanceof Player)) {
sender.sendMessage("\u00a7cThis command can only be used by players!");
return true;
}
Player player = (Player) sender;
if (!player.hasPermission("custommusicdiscs.load")) {
player.sendMessage("\u00a7cYou don't have permission to use this command!");
return true;
}
player.sendMessage("\u00a7aScanning for music files...");
File musicFolder = new File(this.getDataFolder(), "music");
File[] files = musicFolder.listFiles((dir, name) -> name.toLowerCase().endsWith(".ogg"));
if (files == null || files.length == 0) {
player.sendMessage("\u00a7cNo .ogg files found in the music folder!");
player.sendMessage("\u00a7ePath: " + musicFolder.getAbsolutePath());
return true;
}
int loaded = 0;
int skipped = 0;
for (File file : files) {
String fileName = file.getName().replace(".ogg", "");
String[] parts = fileName.split("-");
if (parts.length != 3) {
player.sendMessage("\u00a7eSkipped: " + fileName + " (Invalid format)");
++skipped;
continue;
}
String artist = parts[0].trim();
String songName = parts[1].trim();
String album = parts[2].trim();
ItemStack disc = this.customDiscItem.createDisc(artist, songName, album, fileName);
if (player.getInventory().firstEmpty() != -1) {
player.getInventory().addItem(disc);
player.sendMessage("\u00a7aLoaded: \u00a7f" + artist + " - " + songName);
++loaded;
continue;
}
player.sendMessage("\u00a7cInventory full! Cannot add: " + artist + " - " + songName);
++skipped;
}
player.sendMessage("\u00a7a\u00a7l" + loaded + " \u00a7amusic disc(s) loaded!");
if (skipped > 0) {
player.sendMessage("\u00a7e\u00a7l" + skipped + " \u00a7efile(s) skipped.");
}
return true;
}
if (command.getName().equalsIgnoreCase("reloaddrops")) {
if (!sender.hasPermission("custommusicdiscs.reload")) {
sender.sendMessage("\u00a7cYou don't have permission to use this command!");
return true;
}
this.dropManager.reload();
sender.sendMessage("\u00a7aDrops configuration reloaded!");
sender.sendMessage("\u00a7aMob drops: " + (this.dropManager.isEnabled() ? "\u00a72ENABLED" : "\u00a7cDISABLED"));
return true;
}
if (command.getName().equalsIgnoreCase("reloadsettings")) {
if (!sender.hasPermission("custommusicdiscs.reload")) {
sender.sendMessage("\u00a7cYou don't have permission to use this command!");
return true;
}
this.reloadSettings();
sender.sendMessage("\u00a7aSettings configuration reloaded!");
sender.sendMessage("\u00a7aSound range: " + this.soundRange + " blocks, min distance between discs: " + this.minDiscDistance + " blocks, volume: " + this.soundVolume);
return true;
}
return false;
}
public MusicDiscManager getDiscManager() {
return this.discManager;
}
public CustomDiscItem getCustomDiscItem() {
return this.customDiscItem;
}
}