Solved Problem with my plugin.

Discussion in 'Plugin Development' started by Kody_Jay, Oct 19, 2016.

Thread Status:
Not open for further replies.
  1. I have made a custom help message plugin, and when I type the commands, it gives me the error message, "An internal error occurs while attempting to perform this command". Please can somebody help with this? Here is my code:

    Code:
    package me.Kody_Jay.MontrealCraftHelp;
    
    import java.io.File;
    import java.util.List;
    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.configuration.file.FileConfigurationOptions;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MontrealCraftHelp extends JavaPlugin
    {
      public void onEnable()
      {
        getLogger().info(getConfig().getString("Prefix") + " by Kody_Jay has been enabled.");
        if (!new File(getDataFolder(), "config.yml").exists()) {
          getConfig().options().copyDefaults(true);
        }
        saveConfig();
      }
    
      public void onDisable()
      {
        getLogger().info(getConfig().getString("Prefix") + " by Kody_Jay has been disabled.");
      }
    
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
        List list = getConfig().getStringList("Lines");
        for (String playerlist : list)
          if ((sender instanceof Player))
          {
            Player player = (Player)sender;
            if (cmd.getName().equalsIgnoreCase("help"))
            {
              player.sendRawMessage(ChatColor.translateAlternateColorCodes('&', playerlist));
            }
            if (cmd.getName().equalsIgnoreCase("?"))
            {
              player.sendMessage(ChatColor.translateAlternateColorCodes('&', playerlist));
            }
          }
        return false;
      }
    }
    On line 33, the word, 'list' is underlined in red, and the error says, "Type mismatch: cannot convert from element type Object to String"

    Here is my plugin.yml:
    Code:
     name: MontrealCraftHelp
    main: me.Kody_Jay.MontrealCraftHelp.MontrealCraftHelp
    version: 1.0
     
    commands:
       help:
         description: Set a custom /help message
       ?:
         description: Set a custom /help message
    Here is my config.yml:
    Code:
    Prefix: ''
    #Set a prefix, only used in console, not in game.
    
    Lines:
    - '&8&m |--------------------------| '
    - '  &7» &e/ah &8» &fDisplays the AuctionHouse GUI.'
    - '  &7» &e/forums &8» &fDisplays the Forums link.'
    - '  &7» &e/website  &8» &fDisplays the Forums link.'
    - '  &7» &e/warp &8» &fDisplays the custom Warp GUI.'
    - '  &7» &e/kit &8» &fDisplays all available kits.'
    - '  &7» &e/vote &8» &fVote for us and receive some great rewards.'
    - '  &7» &e/buy &8» &fPurchase something from our Store.
    - '  &7» &e/ip &8» &fDisplays &dMontreal&fCrafts' IP.
    - '&8&m |--------------------------| '
    # If you guys need help let me know by leaving a review
    # or simply PM'ing me ^_^
    Sound: 'ENDERDRAGON_WINGS'
    #May not work, still in Beta
    #Set the sound that is played when someone does /help
    
    Thanks. :)
     
  2. Offline

    timtower Administrator Administrator Moderator

    Moved to plugin development.
     
  3. Offline

    Zombie_Striker

    @Kody_Jay
    There is more to that error than just "an internal error occurred". We need to see the full error log in order to help you.

    Two things:
    1. That error is there because you are treating an object (you do not know if it is a string) as though it is a string. Make sure it is a string, and if it is, then cast it to a string.
    2. You put the for loop in the wrong place. Move the for loop down so it only deals with sending the lines.
    Bukkit already logs your plugins for you. You do not need two messages for your plugin. Delete this line, and the line in the onDisable.
     
  4. Here is my error log.
    Code:
    [13:59:50 WARN]: Unexpected exception while parsing console command "?"
    org.bukkit.command.CommandException: Unhandled exception executing command '?' in plugin MontrealCraftHelp v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-e000104-4cb3258]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-e000104-4cb3258]
    at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot.jar:git-Spigot-e000104-4cb3258]
    at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchServerCommand(CraftServer.java:631) [spigot.jar:git-Spigot-e000104-4cb3258]
    at net.minecraft.server.v1_9_R1.DedicatedServer.aL(DedicatedServer.java:438) [spigot.jar:git-Spigot-e000104-4cb3258]
    at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:401) [spigot.jar:git-Spigot-e000104-4cb3258]
    at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot.jar:git-Spigot-e000104-4cb3258]
    at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot.jar:git-Spigot-e000104-4cb3258]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_74]
    Caused by: java.lang.Error: Unresolved compilation problem:
    Type mismatch: cannot convert from element type Object to String
    
    at me.Kody_Jay.MontrealCraftHelp.MontrealCraftHelp.onCommand(MontrealCraftHelp.java:33) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-e000104-4cb3258]
    ... 8 more
     
  5. Offline

    Zombie_Striker

    @Kody_Jay
    Your issue is that you are not providing the Element the list should hold. Provide the element <String> to the List variable. Cast if necessary.
     
  6. I'm unsure on how to do this, hehe. Please can you provide me the code?
     
  7. Offline

    Zombie_Striker

    @Kody_Jay
    No. Not only is the bukkit forums against spoonfeeding, but this is just basic Java. Just add the element specification (The <String>) to the List object.

    [edit] If you do not know what I am talking about, I would recommend you review Java Collection.
     
  8. I don't mean to come across as rude, but I don't see it as spoonfeeding if I have tried to do it, but couldn't so requested help.

    [edit] Link me please?
     
    Last edited: Oct 19, 2016
  9. Offline

    Zombie_Striker

    @Kody_Jay
    But you did not just request "help" as in to understand what you need to do (which I covered in both posts), but you requests for me to write the line for you. That is why it is spoonfeeding; you want me to write it for you.

    If you only want to store a specific type of object inside a collection (in this case, Strings), you need to provide the type inside the list's element parameters. For creating a List that will only store Strings, change the variable type to List<String>.
     
  10. The thing is, i'm really not sure how I go about doing it
     
  11. @Zombie_Striker I have added the type inside the parameters, which is <String> but now I am receiving this error when loading the plugin:

    Code:
    [14:27:42] [Server thread/INFO]: [MontrealCraftHelp] Enabling MontrealCraftHelp v1.0
    [14:27:43] [Server thread/ERROR]: Cannot load configuration from stream
    org.bukkit.configuration.InvalidConfigurationException: while parsing a block mapping
    in 'string', line 1, column 1:
        Lines:
        ^
    expected <block end>, but found Anchor
    in 'string', line 10, column 4:
        - '&7- &e/ip &8- &fDisplays &dMontr ...
           ^
    
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:56) ~[spigot.jar:git-Spigot-e000104-4cb3258]
        at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:184) ~[spigot.jar:git-Spigot-e000104-4cb3258]
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:239) [spigot.jar:git-Spigot-e000104-4cb3258]
        at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:195) [spigot.jar:git-Spigot-e000104-4cb3258]
        at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:163) [spigot.jar:git-Spigot-e000104-4cb3258]
        at me.Kody_Jay.MontrealCraftHelp.MontrealCraftHelp.onEnable(MontrealCraftHelp.java:15) [MontrealCraftHelp.jar:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) [spigot.jar:git-Spigot-e000104-4cb3258]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-e000104-4cb3258]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-e000104-4cb3258]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.loadPlugin(CraftServer.java:361) [spigot.jar:git-Spigot-e000104-4cb3258]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.enablePlugins(CraftServer.java:321) [spigot.jar:git-Spigot-e000104-4cb3258]
        at net.minecraft.server.v1_9_R1.MinecraftServer.t(MinecraftServer.java:411) [spigot.jar:git-Spigot-e000104-4cb3258]
        at net.minecraft.server.v1_9_R1.MinecraftServer.l(MinecraftServer.java:376) [spigot.jar:git-Spigot-e000104-4cb3258]
        at net.minecraft.server.v1_9_R1.MinecraftServer.a(MinecraftServer.java:331) [spigot.jar:git-Spigot-e000104-4cb3258]
        at net.minecraft.server.v1_9_R1.DedicatedServer.init(DedicatedServer.java:269) [spigot.jar:git-Spigot-e000104-4cb3258]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:527) [spigot.jar:git-Spigot-e000104-4cb3258]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_74]
    Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing a block mapping
    in 'string', line 1, column 1:
        Lines:
        ^
    expected <block end>, but found Anchor
    in 'string', line 10, column 4:
        - '&7- &e/ip &8- &fDisplays &dMontr ...
           ^
    
        at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingKey.produce(ParserImpl.java:570) ~[spigot.jar:git-Spigot-e000104-4cb3258]
        at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:158) ~[spigot.jar:git-Spigot-e000104-4cb3258]
        at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:143) ~[spigot.jar:git-Spigot-e000104-4cb3258]
        at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:224) ~[spigot.jar:git-Spigot-e000104-4cb3258]
        at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:155) ~[spigot.jar:git-Spigot-e000104-4cb3258]
        at org.yaml.snakeyaml.composer.Composer.composeDocument(Composer.java:122) ~[spigot.jar:git-Spigot-e000104-4cb3258]
        at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:105) ~[spigot.jar:git-Spigot-e000104-4cb3258]
        at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:120) ~[spigot.jar:git-Spigot-e000104-4cb3258]
        at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450) ~[spigot.jar:git-Spigot-e000104-4cb3258]
        at org.yaml.snakeyaml.Yaml.load(Yaml.java:369) ~[spigot.jar:git-Spigot-e000104-4cb3258]
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:54) ~[spigot.jar:git-Spigot-e000104-4cb3258]
        ... 16 more
     
    Last edited: Oct 20, 2016
  12. @bwfcwalshy I somewhat know java and bukkit, hence me making plugins. I am just fairly new to it, but I will definitely take a look at these, thank you.
     
    Last edited: Oct 20, 2016
Thread Status:
Not open for further replies.

Share This Page