Convert string to static?

1

Is it possible to convert string to static?

    String prefix = getConfig().getString("prefix");
    String sufix = getConfig().getString("sufix");
  

Can not make a static reference to the non-static prefix field

Is there a way to convert this String to static?

Bukkit.getServer().broadcastMessage("" + prefix + "", 0); 

(I can only get "Prefix" using string)

Full code.

package space.plugin.automsg;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.conversations.PlayerNamePrompt;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;

import com.sun.media.jfxmediaimpl.platform.java.JavaPlatform;

import net.md_5.bungee.api.ChatColor;



public class Main extends JavaPlugin implements Listener{

    public static Automsg plugin;
    public final Logger logger = Logger.getLogger("Minecraft");
    public static int linhaAtual = 0;
    public static int tid = 0;
    public static int circulando = 1;

    String prefix = getConfig().getString("prefix");
    String sufix = getConfig().getString("sufix");




    @Override
    public void onDisable(){
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + " Versão " + pdfFile.getVersion() + " está desabilitado.");

    }

    @Override
    public void onEnable(){

        Bukkit.getPluginManager().registerEvents(this, this);
        this.saveDefaultConfig();

        String prefix = getConfig().getString("prefix");
        String sufix = getConfig().getString("sufix");      
         String delay = getConfig().getString("intervalo");
         long d = Long.parseLong(delay);




        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + " Versão " + pdfFile.getVersion() + " está abilitado.");

    tid  = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
            public void run() {
                try{
                mensagens("plugins/Automsg/mensage.txt");
                } catch (IOException e){

                } 


            }
        }, 0, d); 

        }



    public static void mensagens(String fileName) throws IOException {
        {




        FileInputStream fs;
        fs = new FileInputStream(fileName);
        BufferedReader br = new BufferedReader(new InputStreamReader(fs));
        for(int i = 0; i < linhaAtual; ++i)
            br.readLine();
        String line = br.readLine();
        line = line.replaceAll("&4", ChatColor.DARK_RED + "");
        line = line.replaceAll("&c", ChatColor.RED + "");
        line = line.replaceAll("&6", ChatColor.GOLD + "");
        line = line.replaceAll("&e", ChatColor.YELLOW + "");
        line = line.replaceAll("&2", ChatColor.DARK_GREEN + "");
        line = line.replaceAll("&a", ChatColor.GREEN + "");
        line = line.replaceAll("&b", ChatColor.AQUA + "");
        line = line.replaceAll("&3", ChatColor.DARK_AQUA + "");
        line = line.replaceAll("&1", ChatColor.DARK_BLUE + "");
        line = line.replaceAll("&9", ChatColor.BLUE + "");
        line = line.replaceAll("&d", ChatColor.LIGHT_PURPLE + "");
        line = line.replaceAll("&5", ChatColor.DARK_PURPLE + "");
        line = line.replaceAll("&f", ChatColor.WHITE + "");
        line = line.replaceAll("&7", ChatColor.GRAY + "");
        line = line.replaceAll("&8", ChatColor.DARK_GRAY + "");
        line = line.replaceAll("&0", ChatColor.BLACK + "");
        line = line.replaceAll("&l", ChatColor.BOLD + "");
        line = line.replaceAll("&n", ChatColor.UNDERLINE + "");
        line = line.replaceAll("&o", ChatColor.ITALIC + "");
        line = line.replaceAll("&k", ChatColor.MAGIC + "");
        line = line.replaceAll("&m", ChatColor.STRIKETHROUGH + "");
        line = line.replaceAll("&r", ChatColor.RESET + "");




         //erro, pede que o prefix seja static
        Bukkit.getServer().broadcastMessage("" + prefix + "", 0);
        }

    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event)
    {
        Player player = event.getPlayer();
        player.sendMessage(this.getConfig().getString("entrada"));
    }


}

.

String prefix = getConfig().getString("prefix");
  // pegar a configuração do prefix: .

config.yml

intervalo:
    -10
entrada: 'a'

mensagen:
    -
    -

prefix: '[AutoMsg]'

sufix: '<--'
    
asked by anonymous 01.06.2017 / 19:04

1 answer

4

Your problem is that the method that is using the prefix property is static and this member is non-static .

If you need to access a non-static member (instance member) then the method that does this access can not be static .

Just take the static of the method signature.

public void mensagens(String fileName) throws IOException {
    /* Todo seu código */

    Bukkit.getServer().broadcastMessage("" + prefix + "", 0);
}
    
01.06.2017 / 19:11