<commit_before>package aeronicamc.mods.mxtune.managers;

import aeronicamc.mods.mxtune.Reference;
import aeronicamc.mods.mxtune.blocks.IMusicPlayer;
import aeronicamc.mods.mxtune.blocks.IPlacedInstrument;
import aeronicamc.mods.mxtune.network.PacketDispatcher;
import aeronicamc.mods.mxtune.network.messages.PlayBlockMusicMessage;
import aeronicamc.mods.mxtune.network.messages.PlaySoloMessage;
import aeronicamc.mods.mxtune.network.messages.StopPlayIdMessage;
import aeronicamc.mods.mxtune.util.IInstrument;
import aeronicamc.mods.mxtune.util.SheetMusicHelper;
import aeronicamc.mods.mxtune.util.ValidDuration;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

import static aeronicamc.mods.mxtune.util.SheetMusicHelper.formatDuration;


@SuppressWarnings("unused")
public final class PlayManager
{
    private static final Logger LOGGER = LogManager.getLogger(PlayManager.class);
    private static final Set<Integer> activePlayIds = new HashSet<>();
    private static final Map<Integer, ActiveTune> playIdToActiveTune = new HashMap<>();
    private static final Map<Integer, Integer> playIdToEntityId = new HashMap<>();
    private static final Map<Integer, Integer> entityIdToPlayId = new HashMap<>();

    private PlayManager()
    {
        /* NOP */
    }

    private static int getNextPlayID()
    {
        return PlayIdSupplier.PlayType.PLAYERS.getAsInt();
    }


    /**
     * For playing music from an Item
     * @param playerIn who is playing
     * @return a unique play id
     */
    public static int playMusic(PlayerEntity playerIn)
    {
        return playMusic(playerIn, null, false);
    }

    /**
     * For playing music from a block, e.g. Band Amp.
     * @param world the world of course
     * @param blockPos position of block instrument
     * @return a unique play id
     */
    public static int playMusic(World world, BlockPos blockPos)
    {
        int playId = PlayIdSupplier.INVALID;
        IMusicPlayer musicPlayer;
        if (world.getBlockState(blockPos).getBlock() instanceof IMusicPlayer)
        {
            musicPlayer = (IMusicPlayer) world.getBlockEntity(blockPos);
            if (musicPlayer != null)
            {
                String musicText = musicPlayer.getMML();
                if (musicText.contains("MML@"))
                {
                    ValidDuration validDuration = SheetMusicHelper.validateMML(musicText);
                    if (validDuration.isValidMML())
                    {
                        playId = getNextPlayID();<commit_msg>add TODO: see if I can attach a music playing entity to the tile entity and use that as the music source.<commit_after>package aeronicamc.mods.mxtune.managers;

import aeronicamc.mods.mxtune.Reference;
import aeronicamc.mods.mxtune.blocks.IMusicPlayer;
import aeronicamc.mods.mxtune.blocks.IPlacedInstrument;
import aeronicamc.mods.mxtune.network.PacketDispatcher;
import aeronicamc.mods.mxtune.network.messages.PlayBlockMusicMessage;
import aeronicamc.mods.mxtune.network.messages.PlaySoloMessage;
import aeronicamc.mods.mxtune.network.messages.StopPlayIdMessage;
import aeronicamc.mods.mxtune.util.IInstrument;
import aeronicamc.mods.mxtune.util.SheetMusicHelper;
import aeronicamc.mods.mxtune.util.ValidDuration;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

import static aeronicamc.mods.mxtune.util.SheetMusicHelper.formatDuration;


@SuppressWarnings("unused")
public final class PlayManager
{
    private static final Logger LOGGER = LogManager.getLogger(PlayManager.class);
    private static final Set<Integer> activePlayIds = new HashSet<>();
    private static final Map<Integer, ActiveTune> playIdToActiveTune = new HashMap<>();
    private static final Map<Integer, Integer> playIdToEntityId = new HashMap<>();
    private static final Map<Integer, Integer> entityIdToPlayId = new HashMap<>();

    private PlayManager()
    {
        /* NOP */
    }

    private static int getNextPlayID()
    {
        return PlayIdSupplier.PlayType.PLAYERS.getAsInt();
    }


    /**
     * For playing music from an Item
     * @param playerIn who is playing
     * @return a unique play id
     */
    public static int playMusic(PlayerEntity playerIn)
    {
        return playMusic(playerIn, null, false);
    }

    /**
     * For playing music from a block, e.g. Band Amp.
     * @param world the world of course
     * @param blockPos position of block instrument
     * @return a unique play id
     */
    public static int playMusic(World world, BlockPos blockPos)
    {
        // TODO: see if I can attach a music playing entity to the tile entity and use that as the music source.
        int playId = PlayIdSupplier.INVALID;
        IMusicPlayer musicPlayer;
        if (world.getBlockState(blockPos).getBlock() instanceof IMusicPlayer)
        {
            musicPlayer = (IMusicPlayer) world.getBlockEntity(blockPos);
            if (musicPlayer != null)
            {
                String musicText = musicPlayer.getMML();
                if (musicText.contains("MML@"))
                {
                    ValidDuration validDuration = SheetMusicHelper.validateMML(musicText);
                    if (validDuration.isValidMML())
                    {
                        playId = getNextPlayID();
{'fraction_non_alphanumeric': 0.0922642397614938, 'fraction_numerical': 0.0006276478895339715, 'mean_word_length': 3.3657534246575342, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4822719', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1858, 'n_words': 427}