A

Make me a plugin that adds a new music disc to the game called "Mundian to Bach Ke". It will play the song that is on the YouTube video and works the same way as any other music disc. Only, it can only be optained through "/give". It looks like the music disc "Lena Raine - Creator" only the outside disc is red.

Kodari AI
KodariJul 14 at 9:25 am 10
MundianMusicDisc
A Paper-compatible Minecraft plugin adds a custom Mundian to Bach Ke music disc based on the vanilla Lena Raine – Creator disc. Players obtain it only through the standard `/give <player> mundian` syntax, while normal gameplay cannot craft, find, or generate it. The plugin handles jukebox insertion, playback, ejection, block breaking, automatic song completion, custom item metadata, resource-pack delivery, and a custom model/audio namespace so the disc can use a red outer design and the supplied song recording.
  • Uses the vanilla Creator disc as the custom item base.

  • Intercepts /give <player> mundian and blocks normal acquisition paths.

  • Adds jukebox playback, ejection, cleanup, and automatic stopping.

  • Includes a resource-pack template for the custom model and audio namespace.

The plugin code is complete, but the YouTube recording and custom red disc texture must be supplied as binary resource-pack assets:

  • resourcepack/assets/mundian/sounds/music/mundian_to_bach_ke.ogg

  • resourcepack/assets/mundian/textures/item/mundian_disc.png

Set resource-pack.url in config.yml to the hosted ZIP URL after adding those assets.

Kodari can make mistakes. Review important code before using.
Files
resourcepack
src
build.gradle
settings.gradle
Code
9 406
src/main/java/com/kodari/mundianmusicdisc/MundianMusicDiscPlugin.java
package com.kodari.mundianmusicdisc;

import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Jukebox;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.event.block.Action;
import org.bukkit.NamespacedKey;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public final class MundianMusicDiscPlugin extends JavaPlugin implements Listener {
    private static final int CUSTOM_MODEL_DATA = 24001;
    private static final String DISC_NAME = "Mundian to Bach Ke";

    private NamespacedKey discKey;
    private String soundKey;
    private long songDurationMillis;
    private final Map<Location, Long> playingDiscs = new HashMap<>();

    @Override
    public void onEnable() {
        saveDefaultConfig();

        discKey = new NamespacedKey(this, "mundian_to_bach_ke");
        soundKey = getConfig().getString("song.sound", "mundian:mundian_to_bach_ke");
        songDurationMillis = Math.max(1, getConfig().getLong("song.duration-seconds", 238)) * 1000L;

        Bukkit.getPluginManager().registerEvents(this, this);

        Bukkit.getScheduler().runTaskTimer(this, () -> {
            long now = System.currentTimeMillis();
            Iterator<Map.Entry<Location, Long>> iterator = playingDiscs.entrySet().iterator();

            while (iterator.hasNext()) {
                Map.Entry<Location, Long> entry = iterator.next();

                if (now >= entry.getValue()) {
                    stopSoundAt(entry.getKey());
                    iterator.remove();
                }
            }
        }, 20L, 20L);
    }

    @Override
    public void onDisable() {
        for (Location location : new ArrayList<>(playingDiscs.keySet())) {
            stopSoundAt(location);
        }

        playingDiscs.clear();
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onCommand(PlayerCommandPreprocessEvent event) {
        String message = event.getMessage().trim();

        if (!message.startsWith("/") || message.length() <= 1) {
            return;
        }

        String[] arguments = message.substring(1).split("\\s+");

        if (arguments.length < 3 || !isGiveCommand(arguments[0])) {
            return;
        }

        if (!isMundianName(arguments[2])) {
            return;
        }

        CommandSender sender = event.getPlayer();

        if (!sender.hasPermission("mundian.give")) {
            event.setCancelled(true);
            sender.sendMessage(color("&cYou do not have permission to use this command."));
            return;
        }

        Player target = findTarget(sender, arguments[1]);

        if (target == null) {
            event.setCancelled(true);
            sender.sendMessage(color("&cThat player must be online."));
            return;
        }

        int amount = 1;

        if (arguments.length >= 4) {
            try {
                amount = Integer.parseInt(arguments[3]);
            } catch (NumberFormatException exception) {
                event.setCancelled(true);
                sender.sendMessage(color("&cAmount must be a whole number."));
                return;
            }
        }

        if (amount < 1) {
            event.setCancelled(true);
            sender.sendMessage(color("&cAmount must be at least 1."));
            return;
        }

        event.setCancelled(true);

        for (int index = 0; index < amount; index++) {
            HashMap<Integer, ItemStack> leftovers = target.getInventory().addItem(createDisc(1));

            for (ItemStack leftover : leftovers.values()) {
                target.getWorld().dropItemNaturally(target.getLocation(), leftover);
            }
        }

        sender.sendMessage(color("&aGave &f" + amount + " &aMundian to Bach Ke disc(s) to &f" + target.getName() + "&a."));
    }

    @EventHandler(ignoreCancelled = true)
    public void onJoin(PlayerJoinEvent event) {
        String resourcePackUrl = getConfig().getString("resource-pack.url", "");

        if (resourcePackUrl != null && !resourcePackUrl.isBlank()) {
            event.getPlayer().setResourcePack(resourcePackUrl);
        }
    }

    @EventHandler(ignoreCancelled = true)
    public void onInteract(PlayerInteractEvent event) {
        if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getClickedBlock() == null) {
            return;
        }

        if (event.getHand() == null || !"HAND".equals(event.getHand().name())) {
            return;
        }

        Block block = event.getClickedBlock();

        if (!isJukebox(block)) {
            return;
        }

        Location location = block.getLocation();
        Jukebox jukebox = (Jukebox) block.getState();
        ItemStack heldItem = event.getPlayer().getInventory().getItemInMainHand();

        if (playingDiscs.containsKey(location)) {
            event.setCancelled(true);
            ejectDisc(event.getPlayer(), location, jukebox);
            return;
        }

        if (!isMundianDisc(heldItem) || !isEmpty(jukebox.getRecord())) {
            return;
        }

        event.setCancelled(true);

        ItemStack insertedDisc = heldItem.clone();
        insertedDisc.setAmount(1);

        if (heldItem.getAmount() <= 1) {
            event.getPlayer().getInventory().setItemInMainHand(null);
        } else {
            heldItem.setAmount(heldItem.getAmount() - 1);
        }

        jukebox.setRecord(insertedDisc);
        jukebox.update(true, false);

        location.getWorld().playSound(location, soundKey, 4.0f, 1.0f);
        playingDiscs.put(location.clone(), System.currentTimeMillis() + songDurationMillis);
    }

    @EventHandler(ignoreCancelled = true)
    public void onBlockBreak(BlockBreakEvent event) {
        Block block = event.getBlock();