Class IndexFile

java.lang.Object
de.mrjulsen.dragnsounds.core.filesystem.IndexFile
All Implemented Interfaces:
de.mrjulsen.mcdragonlib.data.INBTSerializable, AutoCloseable

public class IndexFile extends Object implements de.mrjulsen.mcdragonlib.data.INBTSerializable, AutoCloseable
A file containing information about all custom sound files in the same directory. Files that are not registered in this index file cannot be accessed.
  • Field Details

  • Constructor Details

    • IndexFile

      public IndexFile(SoundLocation location, boolean readOnly) throws IOException
      Get the index file of the given location.
      Parameters:
      location - The target directory location.
      Throws:
      IOException
  • Method Details

    • checkAndRepair

      public void checkAndRepair()
      Checks all entries and removes files that no longer exist at the current location.
    • getLocation

      public SoundLocation getLocation()
      Returns:
      The location of this file.
    • add

      public void add(SoundFile file)
      Registers the given sound file in the index file so it can be accessed later. This is very important, otherwise the sound file gets lost and may be deleted.
      Parameters:
      file - The sound file to register.
      API Note:
      Cannot be used in read-only mode!
    • has

      public boolean has(String soundId)
      Checks if a sound file with the given id exists.
      Parameters:
      soundId - The id of the custom sound file you want to check.
      Returns:
      true if the file exists, false otherwise.
    • has

      public boolean has(File file)
      Checks if the sound file exists.
      Parameters:
      file - The file you want to check.
      Returns:
      true if the file exists, false otherwise.
    • delete

      public boolean delete(String soundId)
      Deletes the sound file from the disk and from the index file.
      Parameters:
      soundId - The id of the custom sound file.
      Returns:
      Whether the action was successfull.
      API Note:
      Cannot be used in read-only mode!
    • getSoundFile

      public SoundFile getSoundFile(String soundId)
      Get the sound file with the given id at the current location of the index file.
      Parameters:
      soundId - The id of the cusotm sound file.
      Returns:
      The SoundFile if available, null otherwise.
    • getAll

      public SoundFile[] getAll()
      Returns:
      A list of all registered sound files that also exist on disk.
    • isReadOnly

      public boolean isReadOnly()
      Returns:
      If this instance of the file is read-only.
    • save

      public void save()
      Saves the changed index file to disk. Run this every time after changing some values. You can also use try-with-resources which saves the changed index file automatically.
      API Note:
      Cannot be used in read-only mode!
    • open

      public static IndexFile open(SoundLocation location, boolean readOnly) throws IOException
      Open the index file at the given location. If no index file exists, a new one will be created (only in memory, not on disk).
      Parameters:
      location - The location of the index file.
      readOnly - If the instance should be read-only. Use this whenever possible to make the file accessible for other processes.
      Returns:
      The index file.
      Throws:
      IOException
    • existsIn

      public static boolean existsIn(SoundLocation location)
      Checks if an index file exists at the given location.
      Parameters:
      location - The location to check.
      Returns:
      true if there is an index file at this location.
    • generateId

      public String generateId()
      Generates a new id that doesn't already exist at this location.
      Returns:
      The new id.
    • serializeNbt

      public net.minecraft.nbt.CompoundTag serializeNbt()
      Specified by:
      serializeNbt in interface de.mrjulsen.mcdragonlib.data.INBTSerializable
    • deserializeNbt

      public void deserializeNbt(net.minecraft.nbt.CompoundTag nbt)
      Specified by:
      deserializeNbt in interface de.mrjulsen.mcdragonlib.data.INBTSerializable
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • count

      public int count()
      Returns:
      The amount of registered files.