Class ClientFile
java.lang.Object
net.risingworld.api.objects.ClientFile
Represents a file on the hard drive of a player, i.e. a file the player has selected in the file browser
(see
A client file stays valid until the player disconnects or the plugins are reloaded (see
Things to keep in mind:
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
-
Method Summary
Modifier and TypeMethodDescriptionvoidCancels 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").intgetID()Gets the internal ID of this file.longGets the date when the file was modified the last time on the player's computer.getName()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).longgetSize()Gets the size of the file (bytes).longGets the number of bytes which were received (and written) by the server so far during the active upload.booleanGets whether or not this file is currently being uploaded.booleanisValid()Gets whether or not this file is still valid.toString()voidupload(String targetDirectory, long maxBytes, Callback<PlayerFileUploadEvent> callback) Uploads this file from the player's computer into the provided directory on the server.voidupload(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 aPlayerFileUploadEventis triggered (both receive the same event object).
-
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
Gets the player who selected this file (i.e. the player the file belongs to).- Returns:
- the player who selected this file.
-
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
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
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 (seeUtils.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
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 withPlayerFileUploadEvent.Result.AlreadyExiststhen).
Once the upload is finished (or failed), the callback is invoked and aPlayerFileUploadEventis 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 aPlayerFileUploadEventis 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 (seePlayerFileUploadEvent.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 withPlayerFileUploadEvent.Result.Cancelledthen. 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 withgetSize()this can be used to calculate the progress.- Returns:
- the number of received bytes, or 0 if no upload is active.
-
toString
-