Class ClientFile

java.lang.Object
net.risingworld.api.objects.ClientFile

public final class ClientFile extends Object
Represents a file on the hard drive of a player, i.e. a file the player has selected in the file browser (see Player.showFileBrowser(java.lang.String, java.lang.String[], net.risingworld.api.callbacks.Callback)). The server never gets to know the actual path of the file on the player's computer, it only receives the meta data (name, size, modification date and checksum).
A client file stays valid until the player disconnects or the plugins are reloaded (see isValid()), so the upload does not have to be started immediately (e.g. you can ask the player for confirmation first).

Things to keep in mind:
  • Only one upload per player can be active at the same time.
  • The file size is limited by the plugin (maxBytes) and by the server (server.properties: Plugins_MaxFileUploadSize, in MB, 0 disables uploads). The lower limit applies.
  • Files which exceed the limit of the file browser have no checksum (see PlayerSelectFileEvent.isTooLarge()) and cannot be uploaded.
  • While the upload is running, the player sees a progress indicator; other dialogs (e.g. another file browser) are shown after the upload finished.
  • If the player does not send any data for 30 seconds, the upload fails with PlayerFileUploadEvent.Result.Timeout.
  • Consoles have no file system, the file browser is aborted immediately there.
See Also:

Example: Let the player upload an image, skip the upload if the server already has it
1player.showFileBrowser("Select an image", new String[]{"png", "jpg"}, 5 * 1024 * 1024, (PlayerSelectFileEvent selectEvent) -> {
2 if (selectEvent.isAborted()) return;
3 ClientFile file = selectEvent.getFile();
4 if (selectEvent.isTooLarge()) {
5 player.showStatusMessage(file.getName() + " is too large (max 5 MB)", 3);
6 return;
7 }
8
9 //Name the target file after its checksum, so identical files are only stored once
10 File target = new File(getPath() + "/Images/" + file.getChecksum() + "." + file.getExtension());
11 if (target.exists()) {
12 System.out.println("Image already exists: " + target);
13 return;
14 }
15
16 file.upload(target.getParent(), target.getName(), false, 5 * 1024 * 1024, (PlayerFileUploadEvent uploadEvent) -> {
17 if (uploadEvent.isSuccessful()) System.out.println("Uploaded: " + uploadEvent.getTargetFile());
18 else player.showStatusMessage("Upload failed: " + uploadEvent.getResult(), 3);
19 });
20});
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Cancels the active upload of this file.
    Gets the MD5 checksum of the file (lower case hex string).
    Gets the file extension in lower case, without the leading dot (e.g. "fbx").
    int
    Gets the internal ID of this file.
    long
    Gets the date when the file was modified the last time on the player's computer.
    Gets the name of the file, including the file extension (e.g. "house.fbx").
    Gets the player who selected this file (i.e. the player the file belongs to).
    long
    Gets the size of the file (bytes).
    long
    Gets the number of bytes which were received (and written) by the server so far during the active upload.
    boolean
    Gets whether or not this file is currently being uploaded.
    boolean
    Gets whether or not this file is still valid.
     
    void
    upload(String targetDirectory, long maxBytes, Callback<PlayerFileUploadEvent> callback)
    Uploads this file from the player's computer into the provided directory on the server.
    void
    upload(String targetDirectory, String targetName, boolean overwrite, long maxBytes, Callback<PlayerFileUploadEvent> callback)
    Uploads this file from the player's computer into the provided directory on the server.
    Once the upload is finished (or failed), the callback is invoked and a PlayerFileUploadEvent is triggered (both receive the same event object).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • getID

      public int getID()
      Gets the internal ID of this file. Note that this ID is only valid during the session!
      Returns:
      the internal ID of this file.
    • getPlayer

      public Player getPlayer()
      Gets the player who selected this file (i.e. the player the file belongs to).
      Returns:
      the player who selected this file.
    • getName

      public String getName()
      Gets the name of the file, including the file extension (e.g. "house.fbx"). Never contains any directory information.
      Returns:
      the file name, including the extension.
    • getExtension

      public String getExtension()
      Gets the file extension in lower case, without the leading dot (e.g. "fbx").
      Returns:
      the file extension, or an empty string if the file has no extension.
    • getSize

      public long getSize()
      Gets the size of the file (bytes).
      Returns:
      the file size in bytes.
    • getLastModified

      public long getLastModified()
      Gets the date when the file was modified the last time on the player's computer.
      Returns:
      a timestamp in milliseconds (unix time).
    • getChecksum

      public String getChecksum()
      Gets the MD5 checksum of the file (lower case hex string). This can be used to find out whether or not the server already has a copy of this file (see Utils.FileUtils.getMd5(java.io.File)), so you don't have to upload it again.
      Returns:
      the MD5 checksum of the file, or null if the file exceeded the size limit of the file browser (see PlayerSelectFileEvent.isTooLarge()).
    • isValid

      public boolean isValid()
      Gets whether or not this file is still valid. A file becomes invalid once the player disconnects or the plugins are reloaded.
      Returns:
      true if the file is still valid, false if not.
    • upload

      public void upload(String targetDirectory, long maxBytes, Callback<PlayerFileUploadEvent> callback)
      Uploads this file from the player's computer into the provided directory on the server. The file keeps its name (cleaned up, i.e. without invalid characters). An existing file with the same name will not be overwritten (the upload fails with PlayerFileUploadEvent.Result.AlreadyExists then).
      Once the upload is finished (or failed), the callback is invoked and a PlayerFileUploadEvent is triggered (both receive the same event object). If the upload is not possible at all (e.g. the file is too large), this may happen immediately. Only one upload per player can be active at the same time.
      Parameters:
      targetDirectory - the directory on the server where the file should be stored, e.g. getPath() + "/Uploads". The directory will be created if it does not exist.
      maxBytes - the max allowed file size (bytes). The upper limit is set by the server (server.properties: Plugins_MaxFileUploadSize). Set to 0 to use the limit of the server.
      callback - optional callback which is invoked once the upload is finished. May be null.
      Throws:
      IllegalStateException - if the file is no longer valid or another upload of this player is still active.
      IllegalArgumentException - if the target directory is null, empty or not a valid path.
      See Also:
    • upload

      public void upload(String targetDirectory, String targetName, boolean overwrite, long maxBytes, Callback<PlayerFileUploadEvent> callback)
      Uploads this file from the player's computer into the provided directory on the server.
      Once the upload is finished (or failed), the callback is invoked and a PlayerFileUploadEvent is triggered (both receive the same event object). If the upload is not possible at all (e.g. the file is too large), this may happen immediately. Only one upload per player can be active at the same time.
      Parameters:
      targetDirectory - the directory on the server where the file should be stored, e.g. getPath() + "/Uploads". The directory will be created if it does not exist.
      targetName - the file name on the server (including extension), or null to keep the original name. Invalid characters and directory information will be removed.
      overwrite - true to overwrite an existing file, false to cancel the upload in this case (see PlayerFileUploadEvent.Result.AlreadyExists).
      maxBytes - the max allowed file size (bytes). The upper limit is set by the server (server.properties: Plugins_MaxFileUploadSize). Set to 0 to use the limit of the server.
      callback - optional callback which is invoked once the upload is finished. May be null.
      Throws:
      IllegalStateException - if the file is no longer valid or another upload of this player is still active.
      IllegalArgumentException - if the target directory is null, empty or not a valid path.
    • cancelUpload

      public void cancelUpload()
      Cancels the active upload of this file. The upload fails with PlayerFileUploadEvent.Result.Cancelled then. Does nothing if this file is not being uploaded.
    • isUploading

      public boolean isUploading()
      Gets whether or not this file is currently being uploaded.
      Returns:
      true if an upload of this file is active, false if not.
    • getUploadedBytes

      public long getUploadedBytes()
      Gets the number of bytes which were received (and written) by the server so far during the active upload. Together with getSize() this can be used to calculate the progress.
      Returns:
      the number of received bytes, or 0 if no upload is active.
    • toString

      public String toString()
      Overrides:
      toString in class Object