Spigot Plugin, nobody on Server - plugins

I try to make a spigot plugin that can detect if nobody is on the server. I need this for a timer, if nobody is on the server, it should stop the timer and save the time of the timer in a text file. Is there any way I can do this with a plugin.
Thanks for every help, Aaron.

How to create text files:
try {
File file = new File(Yourplugin.getPlugin(YourPlugin.class).getDataFolder().getPath(), "filename.txt");
file.getParentFile().mkdirs();
file.createNewFile();
} catch (IOException exception){
System.out.println(exception.toString());
}
How to read from text files:
File file = new File(YourPlugin.getPlugin(Yourplugin.class).getDataFolder().getPath(), "filename.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readline(); //can be null, if nothing is in file
How to write to Files:
File file = new File(YourPlugin.getPlugin(Yourplugin.class).getDataFolder().getPath(), "filename.txt");
BufferedWriter wr = new BufferedWriter(new FileWriter(file));
br.write("yourtime");
You should make a try catch construction in every example like in the first one.

See Bukkit#getOnlinePlayers(); and check if it is empty. You can check when a player disconnects from the server, or you can do a repeating scheduled task.
To save the timer in a yml file, use YamlConfiguration. See here for more details : https://www.spigotmc.org/wiki/config-files/#creating-the-file
If you need more help (and a quicker answer, probably) you can add me on discord: Pierre#7757. I'll edit this comment if we find any good solution.

Related

how to Securely write saved data in unity game

I try to make savefile as follows;
string savedata ="aaaa"; //crypted string
StreamWriter writer = new StreamWriter(Path, false);
writer.WriteLine(savedata);
writer.Flush();
writer.Close();
But in this way ,if you exit the application before the writing is finished, you may not be able to save the file.
How can we address this issue?
If i understand right, maybe you could make the save in the "OnApplicationQuit()". I think even if the user decide to leave unity will execute this function and wait for it to finish. So your game will be saved anyway and you don't have to change your code much.

Data overwritten when opening file using FatFs

If I close a file and then reopen it, I cannot write more data to it after reopening it, but if i keep it open i can write as many lines as i want then close it when i am finish writing.
See the example below. Thanks.
if (f_mount(&FatFs, "", 1) == FR_OK) {
f_mkdir ("TEST");
count = 0;
while(count < 200){
if(f_open(&fil, "TEST/test.txt", FA_OPEN_ALWAYS | FA_WRITE) != FR_OK){
break;
}
else{
sprintf(array,"This is file entry number: %d\r\n",count);
f_puts(array, &fil);
if(f_close(&fil) != FR_OK){
break;
}
}
count++;
}
f_mount(0, "", 1);
}
It will count to the max value but it will only write the last entry which is 199.
You need to set your open mode so that it appends to the file rather than writing at the start:
From f_open
FA_OPEN_APPEND Same as FA_OPEN_ALWAYS except read/write pointer is set end of the file.
When you open the file with this:
f_open(&fil, "TEST/test.txt", FA_OPEN_ALWAYS | FA_WRITE);
you are opening the file for writing with the write pointer at the start of the file, so when you go to write to the file with:
f_puts(array, &fil);
you overwrite the previous data in the file.
If you change your open to:
f_open(&fil, "TEST/test.txt", FA_OPEN_APPEND | FA_WRITE);
then you should get the behavior you desire. There is an exception, though, and that's that each time running this, you will continue appending to the file. If that isn't desired, you may need to delete the file first or open it initially with FA_OPEN_ALWAYS and then re-open each pass with FA_OPEN_APPEND.
Depending on what you are trying to do, you should take a look at f_sync, which will perform all clean up and writes that an f_close would perform, but keeps the file open. From the documentation:
This is suitable for the applications that open files for a long time in write mode, such as data logger. Performing f_sync function of periodic or immediataly after f_write function can minimize the risk of data loss due to a sudden blackout or an unintentional media removal.
This would cover nearly every case I can think of for why you might be repeatedly opening and closing a file to append data, so this may be a better solution to your problem.

Titanium Convert TiFile Object to TiFileSystemFile Object

How to save the recorded audio file which is recorded using sound_record.js from Titanium KickenSink Source.
We have,
var file // global variable.
file = recording.stop(); // file will have recorded content which we will convert into media.sound in order to play.
var newDir = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,'audioclips');
Ti.API.info("Created mydir: " + newDir.createDirectory());
var newFile = Titanium.Filesystem.getFile(newDir.nativePath,'newfile.wav');
newFile.write(file.read());
But i am able to save the file which is recorded?
I am not getting how to save this recorded file, I dont knw where i am going wrong.
Please Help,
Thanks in Advance.
I am guessing your are having problem with saving the file. In last line just change
newFile.write(file.read());
to
newFile.write(file);
This should do the trick for you.
Hope this helps.
Edited:
For saving the file try this code:
var newFile =Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'your_file.wav');
// Write the data.
newFile.write( file );

How to delete a file while the application is running

I have developed a C# application, in the application the users choose a photo for each record. However the user should also be able to change the pre-selected photo with a newer one. When the user changes the photo the application first deletes the old photo from the application directory then copies the new photo, but when it does that the application gives an exception because the file is used by the application so it cannot be deleted while the application is running. Does any one have a clue how to sort this out? I appreciate your help
This is the exception
The process cannot access the file
'D:\My
Projects\Hawkar'sProject\Software\Application\bin\Debug\Photos\John
Smith.png' because it is being used by
another process.
//defining a string where contains the file source path
string fileSource = Open.FileName;
//defining a string where it contains the file name
string fileName = personNameTextBox.Text + ".png" ;
//defining a string which specifies the directory of the destination file
string fileDest = dir + #"\Photos\" + fileName;
if (File.Exists(fileDest))
{
File.Delete(fileDest);
//this is a picturebox for showing the images
pbxPersonal.Image = Image.FromFile(dir + #"\Photos\" + "No Image.gif");
File.Copy(fileSource, fileDest);
}
else
{
File.Copy(fileSource, fileDest);
}
imageIDTextBox.Text = fileDest;
First of all, you code is not good.
The new image is only copied if there is currently no image (else).
But if there is an old image, you only delete this image, but never copy the newer one (if).
The code should better look like this:
if (File.Exists(fileDest))
{
File.Delete(fileDest);
}
File.Copy(fileSource, fileDest);
imageIDTextBox.Text = fileDest;
This code should work, but if you are getting an exception that the file is already in use, you should check "where" you use the file. Perhaps you are reading the file at program start. Check all parts of your program you are accessing these user files if there are some handles open.
Thanks a lot for your help and sorry for the mistake in the code I just saw it, my original code is just like as you have written but I don't know maybe when I posted accidentally I've put like this. when the application runs there is a picturebox which the image of each record is shown, that is why the application is giving an exception when I want to change the picture because it has been used once by the picturebox , however I've also tried to load another picture to the picture box before deleting the original one but still the same. I've modified the above could if you want to examine it

How to add metadata to WAV file?

I'm looking for some sample code to show me how to add metadata to the wav files we create.
Anyone?
One option is to add your own chunk with a unique id. Most WAV players will ignore it.
Another idea would to be use a labl chunk, associated with a que set at the beginning or end of the file. You'd also need a que chunk. See here for a reference
How to write the data is simple
Write "RIFF".
save the file position.
Write 4 bytes of 0's
Write all the existing chunks. Keep count of bytes written.
Add your chunk. Be sure to get the chunksize right. Keep
count of bytes written.
rewind to the saved position. Write the new size (as a 32-bit
number).
Close the file.
It's slightly more complicated if you are adding things to an existing list chunk, but the same principle applies.
Maybe the nist file format will give you what you want:
NIST
Here is a lib that could help, but im afraid it looks old. NIST Lib
Cant find more useful information right now how exactly to use it, and im afraid the information papers from my company must stay there. :L/
Try code below
private void WaveTag()
{
string fileName = "in.wav";
WaveReadWriter wrw = new WaveReadWriter(File.Open(fileName, FileMode.Open, FileAccess.ReadWrite));
//removes INFO tags from audio stream
wrw.WriteInfoTag(null);
//writes INFO tags into audio stream
Dictionary<WaveInfo, string> tag = new Dictionary<WaveInfo, string>();
tag[WaveInfo.Comments] = "Comments...";
wrw.WriteInfoTag(tag);
wrw.Close();
//reads INFO tags from audio stream
WaveReader wr = new WaveReader(File.OpenRead(fileName));
Dictionary<WaveInfo, string> dir = wr.ReadInfoTag();
wr.Close();
if (dir.Count > 0)
{
foreach (string val in dir.Values)
{
Console.WriteLine(val);
}
}
}
from http://alvas.net/alvas.audio,articles.aspx#id3-tags-for-wave-files
If you examine the wave file spec you'll see that there does not seem to be room for annotations of any kind. An option would be to wrap the wave file with your own format that includes custom information but you would in effect be creating a whole new format that would not be readable by users who do not have your app. But you might be ok with that.