Convert string to Static?

Asked

Viewed 205 times

1

It is possible to be converting string to Static ?

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

Cannot make a Static Reference to the non-static field prefix

There is a way to convert this String to Static?

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

(I can only get the "Prefix" using string)

Complete 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á habilitado.");

    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: '<--'
  • 1

    Static is not a guy, I didn’t understand your doubt.

  • Doesn’t solve it by putting static before String? static String prefix = getConfig().getString("prefix");

  • I need to put the prefix in broadcastMessage... but there only accepts Static, and to get the prefix I have to use String p to access the file. by getConfig();

  • Diego, no. " Cannot make a Static Reference to the non-static method getConfig() from the type Javaplugin"

  • @Natanaelnicolas We need the block code to understand what you’re talking about.

  • Natanael the problem has nothing to do with type, and yes, scope, but this piece of code is vacant to suggest anything.

  • This problem occurs when from within a static method you try to reference a non-static variable. You could augment the code chunk to get a better sense of what you’re trying to do?

  • I edited and entered the complete code

  • All right, young man. Just look at my answer.

Show 4 more comments

1 answer

4


Your problem is that the method that is using the property prefix 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 cannot be static.

Just take the static signature of the method.

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

    Bukkit.getServer().broadcastMessage("" + prefix + "", 0);
}
  • Great worked, was using Static Poruqe Resource Leak br: 'br' is Never closed. usei br.close();

  • @Natanaelnicolas But leave the method as static it’s not right to avoid Resource Leak. The correct thing is to close the resource, as you did.

  • @Amadiu Instantiating a string?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.