Ok, I just learned how to use the commons-net
libraries and I want you to give it a check. I want to send individual player chat logs to a FTP Server, and I am afraid this is incorrect.
package com.blockenton.server.manager.chat;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.inject.Inject;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.Server;
import org.spongepowered.api.event.state.ServerStartedEvent;
import org.spongepowered.api.event.state.ServerStoppingEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.service.command.CommandService;
import org.spongepowered.api.service.config.DefaultConfig;
import org.spongepowered.api.text.TextBuilder;
import org.spongepowered.api.util.event.Subscribe;
import java.io.File;
import java.io.IOException;
import static com.blockenton.server.manager.chat.net.LogChat.*;
@Plugin(id="com.blockenton.server.manager.chat", name="chat-manager", version="0.0.1-DEV-B-0000001")
public class ChatManagerPlugin {
@Inject private Game game;
@Inject private static Logger logger;
@Inject @DefaultConfig(sharedRoot=false) private File defaultConfig;
@Inject @DefaultConfig(sharedRoot=false) private ConfigurationLoader<CommentedConfigurationNode> configManager;
private CommandService cmdService = game.getCommandDispatcher();
private Server server;
private TextBuilder builder;
private FTPClient ftpClient;
private Gson gson = new GsonBuilder()
.disableHtmlEscaping()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.setPrettyPrinting()
.serializeNulls()
.create();
private static Logger getLogger() {
return logger;
}
public static boolean chatDirectoryExists, playerDirectoryExists;
@Subscribe
public void onServerStart(ServerStartedEvent event) {
getLogger().info("Plugin Started");
setIp("184.95.42.202"); //Set the FTP ip
setPassword("YOU DONT NEED TO KNOW"); //Set FTP password
setPort(21); //Set the FTP port
setUsername("frogocomics.313453"); //Set the FTP username
run(ftpClient); //Connect to the FTPServer
chatDirectoryExists = checkForPlayerChatDictionary(ftpClient); //Check for the dictionaries
playerDirectoryExists = checkForPlayerStatsDictionary(ftpClient);
try {
if(!chatDirectoryExists) {
createDictionaries(ftpClient, "\\player-chat");
}
if(!playerDirectoryExists) {
createDictionaries(ftpClient, "\\player-stats");
}
} catch(IOException e) {
getLogger().error("Unable to connect to the database. Stopping the server is advised. Full stack trace:");
getLogger().error(e.getMessage().trim());
}
}
@Subscribe
public void onServerStop(ServerStoppingEvent event) {
getLogger().info("Stopping chat-manager");
}
}
I also included some of my own methods here, so I will describe it to you.
(From: com.blockenton.server.manager.chat.net.LogChat
)
public static void run(FTPClient ftpClient) //Connects to the server
public static boolean checkForPlayerStatsDirectory(FTPClient ftpClient, String dir) //Checks if the Player stats directory exists. dir(String) is the relative path.
public static boolean checkForPlayerChatDirectory(FTPClient ftpClient, String dir) //Look above!
setIp(String ip), setPassword(String password), setPort(int port), setUsername(String username) //Exactly what it says