1

I got this code i made for a Minecraft server, and i have some problems, I think i made it a bit too complicated and i can't figure out what to do now.. I want to save my Teststring and get it into a console log.. + Can someone give me an overview of what i'm doing well and what not? Im a starting developer and i need some opinions please. Thanks in advance.

package be.digibits.tim;

import java.awt.Color;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.server.ServerEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {

    public final Logger logger = Logger.getLogger("Minecraft");
    String teststring = new String();

    @Override
    public void onEnable(){
        createConfig();
        saveConfig();
    }

    @Override
    public void onDisable() {
        saveConfig();
    }

    @EventHandler
    public boolean onCommand(CommandSender cs, Command c, String label, String[]args) {
        Player player = (Player) cs;
        if(c.getName().equalsIgnoreCase("hi")) {
            player.sendMessage("Hi"+ player.getName() + " this plugin is working fine."+ " The message you made appear from the config is: ");
            logger.info("send message to the player..");
            return true;
        }
        return false;
    }

    public void createConfig() {
        File file = new File("plugins/Test_Plugin/config.yml");
        try {
            file.createNewFile();
        } catch(Exception e) {
            logger.info("the test config file already exists.");
        }
        try {
            FileWriter writer = new FileWriter("plugins/Test_Plugin/config.yml");
            writer.write("***********************CONFIG*********************");
            writer.write("\nNAME=" + teststring);
            writer.close();
        } catch (IOException e) {
        } finally {
            if(teststring != "") {
                logger.info( teststring+ "has been added to the config file at NAME");
            }
        }
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • `teststring != ""` don't compare strings with `!=` or `==`. Use `!"foo".equals("bar")`: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Pshemo Apr 05 '15 at 13:07
  • Actually in this particular case you can even use negation of `isEmpty()` method like `if (!teststring.isEmpty()){...}` – Pshemo Apr 05 '15 at 13:11

0 Answers0