How can i create a messages.yml file?

Discussion in 'Plugin Development' started by EyesPlay, Sep 5, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    EyesPlay

    Hi! I'm developing a plugin and i need to have a file with all messages to change it... i don't know how to create another file with different name... can you explain me? or give me an example code?
    thx! and sorry for my bad english...
     
  2. Offline

    Zombie_Striker

  3. Offline

    EyesPlay

    ehi!
    I had already read that page ... but I did not understand how to use it inside a plugin and then add messages to the file and make them accessible from other classes
     
  4. Offline

    Zombie_Striker

    @EyesPlay
    Using the lines in that thread, load your custom yml file. After that, you can set and get any variables you want the same way you do it for the default config. When you are done, call CustomConfig#save(File) to save the config to that file.
     
  5. Offline

    EyesPlay

    @Zombie_Striker ok!
    Code:
            FileConfiguration messages = YamlConfiguration.loadConfiguration(getResource("messages.yml"));
    i've added this to onEnable but i can't access it from other classes... how can i do it?
     
  6. Offline

    Reflxction

  7. Offline

    EyesPlay

    @xTechno_ ehi thanks for the answer! but i'm using custom configuration file... "messages.yml" how can i use this?
     
  8. Offline

    Caderape2

    @EyesPlay Create an acces between classes with constructor, it's basic java. You should learn it.
     
  9. Offline

    Reflxction

    ^
    The getConfig() method gets the main config.yml file. Since it returns FileConfiguration, instead of using the getter just use customconfig.addDefault, getString etc
     
  10. Offline

    EyesPlay

    @xTechno_ thanks! i'm going to test it!

    after some test and an help from a friend i added this to the main class:
    Code:
        File messages;
        FileConfiguration messagesConfig;
    
        @Override
        public void onEnable() {
           
            messages= new File("plugin/TwoStep/messages.yml");
            messagesConfig= YamlConfiguration.loadConfiguration(messages);
            messagesConfig.options().copyDefaults(true);
    but it gives me an error...

    https://imgur.com/a/15atb

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 10, 2017
  11. Offline

    timtower Administrator Administrator Moderator

    @EyesPlay You imported the wrong File class
     
  12. Offline

    EyesPlay

    @timtower (?) no... is the main class...
     
  13. Offline

    timtower Administrator Administrator Moderator

    @EyesPlay there are multiple File classes in 2 different packages.
    You have imported the wrong one.
     
  14. Offline

    EyesPlay

    @timtower
    Code:
    import it.eyesplay.twostep.commands.EnableCommand;
    import it.eyesplay.twostep.commands.LoginCommand;
    import it.eyesplay.twostep.listener.InventoryPinListener;
    import it.eyesplay.twostep.listener.PreventListener;
    import it.eyesplay.twostep.listener.SessionListener;
    import com.google.common.collect.Lists;
    import com.google.common.collect.Maps;
    import com.google.common.collect.Sets;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.craftbukkit.libs.org.ibex.nestedvm.util.Seekable.File;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.Bukkit;
    
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.util.*;
    import java.util.logging.Level;
    this is my imports... the only import that can make mistakes is import java.nio.file.Files...


    edit: i've imported the wrong...

    Code:
    change import org.bukkit.craftbukkit.libs.org.ibex.nestedvm.util.Seekable.File
    with
    import java.io.File;
     
  15. Offline

    timtower Administrator Administrator Moderator

    @EyesPlay Does it work now?
    And that is why you don't build on CraftBukkit but on Bukkit
     
  16. Offline

    EyesPlay

    @timtower yes now it work but when i use getString in chat it appear like "!mapgiver!" and not the message in that string...
    i use spigot 1.8.8 for develop...
     
  17. Offline

    timtower Administrator Administrator Moderator

    @EyesPlay Then please show your config.
    And use the API instead of the server itself. Will save you mistakes like this.
     
  18. Offline

    EyesPlay

    @timtower
    Code:
    # You can Change all the plugin's messages here.
    mapGiver1: &6Ecco il tuo codice segreto. Scannerizzalo con il tuo cellulare
    mapGiver2: &6Poi droppa la mappa per eliminarla
    this is the content of messages.yml

    Code:
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', Messages.getString("mapGiver1")));
    i use this


    edit: where can i found the api?
     
    Last edited: Sep 10, 2017
  19. Offline

    AdamDev

    @EyesPlay
    What are you using to get the string? A command or a chat event?
     
  20. Offline

    EyesPlay

    @AdamDev ? the string is in the messages.yml file.. and i need to use it instead of a message... but it seems to not working...

    edit: i understood!! i can't access the messages.yml from the other classes... how can i do this? an example code would be appreciated xD
     
    Last edited: Sep 10, 2017
  21. Offline

    AdamDev

    @EyesPlay
    No, In-game are you using a command to return that string or are you typing something in chat like "!mapGiver"?
     
  22. Offline

    EyesPlay

    @AdamDev oh yes xD nope... it gives to the player a map and tell to the player in chat the message
     
  23. Offline

    AdamDev

    @EyesPlay
    1. For further reference, I suggest checking out this website for more than one config file. https://www.spigotmc.org/threads/multiple-config-files.263774/
    2. We can't exactly go off of a little bit of code. We have only seen a couple of lines code. We're trying to help you but please send the code where it's giving you a error.
    3. When your giving the map is it already in the server data? Or are you giving them a empty map?
    4. When sending the message "Messages.getConfig().getString("mapGiver1");" is there something like "Messages.getConfig().getString("Messages.mapGiver1")".

    Hope this helps!
    - AdamDev
     
  24. Offline

    EyesPlay

    @AdamDev thanks!
    TwoStep.java:
    Code:
            messages= new File("plugin/TwoStep/messages.yml");
            messagesConfig= YamlConfiguration.loadConfiguration(messages);
            messagesConfig.options().copyDefaults(true);

    MapGiver.java:
    Code:
    public class MapGiver implements Runnable {
    
        private final TwoStep plugin;
        private final Player player;
        private final BufferedImage resultImage;
    
        public MapGiver(TwoStep plugin, Player player, BufferedImage resultImage) {
            this.plugin = plugin;
            this.player = player;
            this.resultImage = resultImage;
        }
    
        @Override
        public void run() {
            MapView createdView = installRenderer(player, resultImage);
            ItemStack mapItem = new ItemStack(Material.MAP, 0, createdView.getId());
            player.getInventory().addItem(mapItem);
            player.sendMessage(ChatColor.translateAlternateColorCodes('&', Messages.getString("mapGiver1")));
     
  25. Offline

    AdamDev

    @EyesPlay
    Is there some command like /givemap <map> ?

    Edit:
    Change "messages = new File("plugin/TowStep/messages.yml");" to "messagss = new File(getDataFolder(), "messages.yml");" makes it simpler.
     
  26. Offline

    EyesPlay

  27. Offline

    AdamDev

    @EyesPlay
    So what's giving that map then?
     
  28. Offline

    EyesPlay

    @AdamDev when a player join if isn't in the database it gives a map
     
  29. Offline

    AdamDev

    @EyesPlay
    I'm gonna get on my computer so I can type a little faster.
     
  30. Offline

    EyesPlay

Thread Status:
Not open for further replies.

Share This Page