Small Error

Discussion in 'Plugin Development' started by DarkLazerOG, Feb 6, 2017.

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

    DarkLazerOG

    I can't seem to be making any progress here... can anyone help me fix this?
    ---------------------------------------------------------------------------------------------------------

    Code:
     @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
    
            getConfig().options().copyDefaults(true);
            saveConfig();
            reloadConfig();
        }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player player = (Player) sender; //Error happens in sender (sender cannot be resolved  to a variable
            List<String> list = getConfig().getStringList("message");
            player.sendMessage(list.toArray(new String[list.size()]));
        }
    ---------------------------------------------------------------------------------------------------------

    I kinda know my mistake but I am not aware on how to solve it!
     
    Last edited by a moderator: Feb 6, 2017
  2. e.getPlayer();

    And please learn some java first, sender is just the variable name that is in the onCommand method's parameters.
     
  3. Offline

    DarkLazerOG

    I apologise for that inconvinience. I started learning how to code 4 days ago and I'm trying to improve since then. I came here believing that someone would help me find my mistake and show it to me. It's obvious that you have much to offer. Share your knowledge with others who want to learn like me.

    My second problem is this:


    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
    Player p = e.getPlayer();
    List<String> list = getConfig().getStringList("message");
    p.sendMessage(list.toArray(new String[list.size()]));
    }

    What do I have to add so the color they put in the "message" is received?
    I tried adding ChatColor.translateAlternateColorCodes(altColorChar, textToTranslate)
    but it did not accept it in any line

    example:

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
    Player p = e.getPlayer();
    List<String> list = ChatColor.translateAlternateColorCodes( '&', getConfig().getStringList("message");
    p.sendMessage(list.toArray(new String[list.size()]));
    }

    or

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
    Player p = e.getPlayer();
    List<String> list = getConfig().getStringList("message");
    p.sendMessage(ChatColor.translateAlternateColorCodes( '&', list.toArray(new String[list.size()])));
    }
     
    Last edited: Feb 6, 2017
  4. Offline

    Zombie_Striker

    @DarkLazerOG
    https://sites.google.com/site/tecnowizardplugindevelopment/java/not-ready-for-bukkit

    https://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.395662/


    This is what you should use, sort of. You forgot to for loop through the list. You can't provide a string list in the parameters. It only accepts string instances. For loop through the list and translate each individual string.
     
  5. Offline

    DarkLazerOG

    I tried adding your code which was similar to my first one but that seems to not be working either. Same error. As for the fact that I'm not ready for bukkit I was aware of that when I started. This is simlpy the start, I wanted to get a look on how it is to code. I'll be learning java after I'm satisfied that I can understand it.
     
  6. Online

    timtower Administrator Administrator Moderator

    @DarkLazerOG Loop through the message list.
    Change the chat color
    Send message
     
  7. Offline

    Rayzr522

  8. Offline

    DarkLazerOG

    Alright, thanks for your support but still can someone correct the code so it accepts color codes?
     
  9. Offline

    mine-care

    @DarkLazerOG That would be classed as spoonfeeding which is something we avoid doing in these forums colectively and will definately not help you engage with Java.
    That is essentially like asking us to make pieces for your own puzzle and although that may work with the puzzle example, in code it doesnt really... Getting pieces of code from here and there, throwing them in a class and somehow bonding them togeather so that they 'work' is not programming and tends to have huge efficiency problems and many possible 'holes'.

    I'm not saying that doing your research online is a bad thing whatsoever, however for the above reason we do not write code for you and we encourage you to look into the docs (Hint: it is there, trust me) and try to find it yourself. Ofcourse for that you would need to know the fundamental Java concepts which trust me are not hard to learn and can also be fun, but for sure they will be usefull in your life regardless of what you want to do ;) <Speaking of experience>
     
    Rayzr522 and Zombie_Striker like this.
Thread Status:
Not open for further replies.

Share This Page