Help with adding %player% to config

Discussion in 'Spigot Plugin Development' started by JJpro321, Jun 26, 2016.

  1. Hello so I have made a plugin and it sends a player a custom message from config. Players can edit the message in config, so I would like them to be able to do something like '&bWelcome %player%' I have set up the color code thing but how do I add %player% so it displays the player's name? CODE:

    Code (Text):
    package com.jjpro321.essentialslite.event.player;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.craftbukkit.v1_9_R2.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import com.jjpro321.essentialslite.Main;

    import net.minecraft.server.v1_9_R2.IChatBaseComponent.ChatSerializer;
    import net.minecraft.server.v1_9_R2.PacketPlayOutTitle;
    import net.minecraft.server.v1_9_R2.PacketPlayOutTitle.EnumTitleAction;
    import net.minecraft.server.v1_9_R2.PlayerConnection;

    public class PlayerJoin implements Listener {
       
         private Main plugin;
       
        public PlayerJoin(Main pl) {
            plugin = pl;
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
             String welcomeMessage = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Welcome Message"));
             String playerJoin = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Player Join"));
             
             Player player = event.getPlayer();
             if(player.hasPlayedBefore())
                 Bukkit.broadcastMessage(playerJoin);
             else
                 Bukkit.broadcastMessage(welcomeMessage);
           
            PacketPlayOutTitle welcomeTitle = new PacketPlayOutTitle(EnumTitleAction.TITLE,
                    ChatSerializer.a("{\"text\":\"Welcome to the server\",\"color\":\"yellow\",\"bold\":true}]"), 20, 40, 30);
            PacketPlayOutTitle welcomeSubtitle = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE,
                    ChatSerializer.a("{\"text\":\"We hope you enjoy your stay!\",\"color\":\"gray\",\"bold\":true}]"), 20, 40, 30);
                   
            PlayerConnection connection = ((CraftPlayer)player).getHandle().playerConnection;
            connection.sendPacket(welcomeTitle);
            connection.sendPacket(welcomeSubtitle);
        }
    }
     
     
  2. #replace(char, string);
    also use a reflection class to avoid nms issues.
     
  3. ummm can you give me an example? Sorry.
     
  4. So this?
    replace(%player%, event.getPlayer());
     
    • Winner Winner x 1
  5. Message = Message.replace("%player%", event.getPlayer().getName());
     
  6. Reflection should be avoided.
     
    • Agree Agree x 1
  7. Ohhh thanks
     
  8. Ummm help? http://prntscr.com/blc72z
     
  9. Are you defining Message as a class? Or are your java conventions incorrect? Show us all the code.
     
  10. I'm just trying to get it so when other people get the plugin they can use %player% to display that players name. CODE:


    Code (Text):
    package com.jjpro321.essentialslite.event.player;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.craftbukkit.v1_9_R2.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import com.jjpro321.essentialslite.Main;

    import net.minecraft.server.v1_9_R2.IChatBaseComponent.ChatSerializer;
    import net.minecraft.server.v1_9_R2.PacketPlayOutTitle;
    import net.minecraft.server.v1_9_R2.PacketPlayOutTitle.EnumTitleAction;
    import net.minecraft.server.v1_9_R2.PlayerConnection;

    public class PlayerJoin implements Listener {
       
         private Main plugin;
       
        public PlayerJoin(Main pl) {
            plugin = pl;
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
             String welcomeMessage = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Welcome Message"));
             String playerJoin = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Player Join"));
             Message = Message.replace("%player%", event.getPlayer().getName());
             
             Player player = event.getPlayer();
             if(player.hasPlayedBefore())
                 Bukkit.broadcastMessage(playerJoin);
             else
                 Bukkit.broadcastMessage(welcomeMessage);
           
            PacketPlayOutTitle welcomeTitle = new PacketPlayOutTitle(EnumTitleAction.TITLE,
                    ChatSerializer.a("{\"text\":\"Welcome to the server\",\"color\":\"yellow\",\"bold\":true}]"), 20, 40, 30);
            PacketPlayOutTitle welcomeSubtitle = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE,
                    ChatSerializer.a("{\"text\":\"We hope you enjoy your stay!\",\"color\":\"gray\",\"bold\":true}]"), 20, 40, 30);
                   
            PlayerConnection connection = ((CraftPlayer)player).getHandle().playerConnection;
            connection.sendPacket(welcomeTitle);
            connection.sendPacket(welcomeSubtitle);
        }
    }
     
     
  11. You have to replace "Message" with the variable name of the welcome Message.
    And learn to programm
     
    • Funny Funny x 1
  12. I'm not even sure what you're doing with Message.
    However, get the welcome message string and call #replaceAll

    Code (Text):

    String example = welcomeMessage.replaceAll("%player%", event.getPlayer().getName());

    Bukkit.broadcastMessage(example);
     
    P.S
    Learn some java before trying to make plugins
     
    • Funny Funny x 1
  13. 1) Thanks
    2) I'm still learning jeez
     
  14. replaceAll takes a regex, use replace!
     
  15. This was not me! I'm sorry but my account was hacked.
     
    • Optimistic Optimistic x 1