Solved Placeholders in plugin

Discussion in 'Spigot Plugin Development' started by Phoenix852, Jan 25, 2020.

Thread Status:
Not open for further replies.
  1. I am making a plugin called proxmessages, I want to get a string from the config.yml and then I want to replace {PLAYER} with playername. Ty :)
     
  2. config.getString("wtv").replaceAll("{PLAYER}", p.getName());

    I recommend looking at a beginners Java tutorial :) Will help with all of this stuff
     
    • Agree Agree x 1
  3. Strahan

    Benefactor

    Bear in mind by hanging a method off a config String retrieval the way you do, if there is something wrong and the "wtv" is not present the plugin will crash. Either pass it a default value in the getString or make it separate statements so you can properly null check it.
     
    • Agree Agree x 1
  4. This^ I was just giving the code to get the string, not for all the scenarios (as I don't love spoonfeeding everything)

    Alternatively surround w/ try and catch
     
  5. Strahan

    Benefactor

    Be that as it may, if you are going to go as far as to put complete code you may as well format it properly; you know 99.999% of the time the receiver will just copy and paste without taking the time to properly understand the functionality behind it heh.
     
  6. Last word I'll say on the topic because I don't wanna derail the thread, while I agree with the second half of your comment, I hardly posted "complete code." I posted one line, without the config path, without checking what the player variable is called, and without catching any errors. Wasn't my intention to write ready to go code, was my intention to provide one line and let them worry about the rest. Which is what I've done. I do agree with the second half of what you said though.
     
  7. I agree with it but, I am not like the other people. I like programming and I go through every code. Anyway...


    Thanks for the support. Nice of you ☺
     
  8. FrostedSnowman

    Resource Staff

    You're not using regex so use String::replace. String::replaceAll is designed for regex use.
     
  9. Can u please explain a bit more about the code?
     
  10. Hello

    Placeholders class:
    Code (Java):

    package com.fighter.bc.Placeholders;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    public class Placeholders implements Listener {
        public String pch(String string, Player player) {
         
            string = ChatColor.translateAlternateColorCodes('&', string);
            string = string.replace("{player-name}", player.getName());
         
            return string;
         
        }
    }
     
    How to use ?
    in every class you can use!
    Code (Java):
    Placeholders pch = new Placeholders();

    player.sendMessage(pch.pch(getConfig().getString("Your string"), player));
     
    • Like Like x 1
  11. Actually please don’t do that, the receiver should know how to do that, it’s basic.
     
    • Agree Agree x 1
  12. Why not? I already said that I go through and try to understand every single code one program has in it.
    Thanks for the help. It is easy as you mentioned an example.
     
  13. I'm glad to help you :)
     
  14. In your case yes, but I've seen so many people extending their mouth towards the spoon you have no idea.
     
    • Agree Agree x 1
  15. Hell yeah, they just copy and paste the code like fuck. Anyway, have a good day!
     
  16. Strahan

    Benefactor

    You didn't understand what I meant. By complete code, I don't mean writing his plugin for him I mean a line that can achieve what he wants, copied and pasted into his IDE. Yea, config path may not be right but config pathing isn't the crux of his issue so that's irrelevant. You yourself admit it's not proper and has flaws. As I said prior, many people just copy and paste and won't take the time to break down the example and fix the areas where it's flawed. I'm just saying; if you are gonna take the time to provide an example, provide a proper one or just use pseudocode.

    I'm going to assume you misquoted and meant what I said later, because that makes more sense unless you really disagree with data validation vs potentially crashable compount methods.

    That's good, but I wasn't speaking with you specifically in mind rather the forum in general.
     
    #16 Strahan, Jan 25, 2020
    Last edited: Jan 25, 2020
  17. @Phoenix852
    If your issue fixed, please change title to Solved!
     
  18. Ohkay!
     
Thread Status:
Not open for further replies.