Skocz do zawartości
  • 0

Lista w configu


Vilor

Pytanie

5 odpowiedzi na to pytanie

Rekomendowane odpowiedzi

  • 0

Jeżeli chodzi o zapisywanie listy do configu (i jej tworzenie), to po prostu tworzysz prosty key i value.

//zapisywanie listy do configu
final List<String> messages = Arrays.asList("first message", "second message");
config.set("messages", messages);
  
//wysylanie listy do gracza
//Player#sendMessage przyjmuje jako parametry String, czyli Liste z configu musisz sprowadzic do stringa, najlatwiej bedzie 
//przy uzyciu String.join
final List<String> messages = config.getStringList("messages");
//pierwszy parametr to "delimiter" czyli w krotkim tlumaczeniu taki 'oddzielacz' wiadomosci,
//zamiast \n (czyli nowej lini) mozesz uzyc np. przecinka
final String message = String.join("\n", messages);
//do gracza zostanie wyslana nastepujaca wiadomosc:
/*
first message
second message
*/
player.sendMessage(message);

zmienna config to po prostu FileConfiguration pobrane np. z twojej klasy glownej rozszerzoną o JavaPlugin - plugin.getConfig()

Odnośnik do komentarza
https://skript.pl/temat/34752-lista-w-configu/#findComment-223387
Udostępnij na innych stronach

  • 0

'Config' mam na czerwono, a messeges mam podkreślone na czerwono

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (sender instanceof Player) {
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("test")) {
                final List<String> messages = Arrays.asList("first message", "second message");
                config.set("messages", messages);
                final List<String> messages = config.getStringList("messages");
                final String message = String.join("\n", messages);
                p.sendMessage(message);

 

Odnośnik do komentarza
https://skript.pl/temat/34752-lista-w-configu/#findComment-223389
Udostępnij na innych stronach

  • 0
1 godzinę temu, Vilor napisał:

'Config' mam na czerwono, a messeges mam podkreślone na czerwono


    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (sender instanceof Player) {
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("test")) {
                final List<String> messages = Arrays.asList("first message", "second message");
                config.set("messages", messages);
                final List<String> messages = config.getStringList("messages");
                final String message = String.join("\n", messages);
                p.sendMessage(message);

 

Masz na czerwono messages - bo tam są dwie różne zmienne (tak samo nazwane), to byl tylko przyklad kodu.

Config masz na czerwono bo wgl nie stworzyles takiej zmiennej, to mialobyc odwolanie do FileConfiguration z glownej klasy tak jak napisales, myslalem ze ogarniesz. Chyba jeszcze troche musisz podstaw sie pouczyc lub poogladaj jakies gotowe pluginy publiczne z dobrym kodem.

public class TestCommand implements CommandExecutor {
  private final FileConfiguration config;
  
  public TestCommand(FileConfiguration config) {
    this.config = config;
  }
  
  	@Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (!(sender instanceof Player)) {
          sender.sendMessage("this command is executable only by player");
          return true;
        }
        Player p = (Player) sender;
      //napisalem ze to ustawia w configu "messages" na jakies wiadomosci, nw po co to tu dales
        /*final List<String> messages = Arrays.asList("first message", "second message");
        config.set("messages", messages);*/
          
       final List<String> messages = config.getStringList("messages");
       final String message = String.join("\n", messages);
       p.sendMessage(message);
    }
}

a w glownej klasie zrob po prostu cos w stylu

public class TestPlugin extends JavaPlugin {
  
  @Override
  public void onEnable() {
    this.saveDefaultConfig();
    
    final TestCommand testCommand = new TestCommand(this.getConfig());
    this.getCommand("test").setExecutor(testCommand);
    
  }
  
}

config.yml ma wygladac tak:

messages:
  - "first message"
  - "second message"

a plugin.yml:

#jakies inne typu main, author, version itp

commands:
  test:
    aliases: [testcommand, testalias]
    description: "Test command"

 

Edytowane przez yooniks
Odnośnik do komentarza
https://skript.pl/temat/34752-lista-w-configu/#findComment-223433
Udostępnij na innych stronach

  • 0
17 godzin temu, yooniks napisał:

Masz na czerwono messages - bo tam są dwie różne zmienne (tak samo nazwane), to byl tylko przyklad kodu.

Config masz na czerwono bo wgl nie stworzyles takiej zmiennej, to mialobyc odwolanie do FileConfiguration z glownej klasy tak jak napisales, myslalem ze ogarniesz. Chyba jeszcze troche musisz podstaw sie pouczyc lub poogladaj jakies gotowe pluginy publiczne z dobrym kodem.


public class TestCommand implements CommandExecutor {
  private final FileConfiguration config;
  
  public TestCommand(FileConfiguration config) {
    this.config = config;
  }
  
  	@Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (!(sender instanceof Player)) {
          sender.sendMessage("this command is executable only by player");
          return true;
        }
        Player p = (Player) sender;
      //napisalem ze to ustawia w configu "messages" na jakies wiadomosci, nw po co to tu dales
        /*final List<String> messages = Arrays.asList("first message", "second message");
        config.set("messages", messages);*/
          
       final List<String> messages = config.getStringList("messages");
       final String message = String.join("\n", messages);
       p.sendMessage(message);
    }
}

a w glownej klasie zrob po prostu cos w stylu


public class TestPlugin extends JavaPlugin {
  
  @Override
  public void onEnable() {
    this.saveDefaultConfig();
    
    final TestCommand testCommand = new TestCommand(this.getConfig());
    this.getCommand("test").setExecutor(testCommand);
    
  }
  
}

config.yml ma wygladac tak:


messages:
  - "first message"
  - "second message"

a plugin.yml:


#jakies inne typu main, author, version itp

commands:
  test:
    aliases: [testcommand, testalias]
    description: "Test command"

 

No trochę muszę się jeszcze poduczyć, dziękuję za pomoc. 

Do zamknięcia :)

Odnośnik do komentarza
https://skript.pl/temat/34752-lista-w-configu/#findComment-223521
Udostępnij na innych stronach

Nieaktywny
Ten temat został zamknięty. Brak możliwości dodania odpowiedzi.
  • Ostatnio przeglądający   0 użytkowników

    • Brak zarejestrowanych użytkowników przeglądających tę stronę.
×
×
  • Dodaj nową pozycję...