1.12.2 Placeholders

Discussion in 'Spigot Plugin Development' started by Gflux, Jul 31, 2020 at 11:46 PM.

  1. So am wanting to create my own placeholders for my plugin. For example %death_who% would display who was killed in a death message. %death_killer% would display who killed them. I have looked in lots of places but can’t find a good way to do this. I have already hooked into PlaceholderAPI but it doesn’t have the things I need. How would I create these placeholders?
     
  2. Send your code, I actually used this today in my plugin so I have the code, but have you tried anything first?
    If not, try this:

    1. Create a new class solely for your placeholder that extends PlaceholderExpansion
    2. Add the unimplemented methods to your class
    3. Changed the methods how you desire (note the identifier is NOT the placeholder, it's normally the plugin name, so if you want %myplugin_kills% to show kills, the identifier would be "myplugin"
    4. create an onPlaceholderRequest method with Player and identifer (String) params
    5. check if identifer == the one you want ("kills") or whatever
    6. return/print result
     
  3. I haven’t done anything for the placeholders yet as I don’t know where to start
     
  4. Read my edited message above ^
    If you struggle, let me know with what part
     
  5. Can you give a bit of code?
     
  6. I don't want to spoonfeed, but I'll give you a little hint
    Code (Java):
    public class Placeholder extends PlaceholderExpansion  {
       
        @Override
        public String getAuthor() {
            //return your name as a String
        }

        @Override
        public String getIdentifier() {
            //return the identifier, as I stated above
        }

        @Override
        public String getVersion() {
            return "FINAL"; //or whatever version your plugin is
        }

        @Override
        public String onPlaceholderRequest(Player p, String identifier) {

            if (identifier.equalsIgnoreCase("my_stat")) {
                // return the stat that you want
            }
        }
       
    }
     
  7. I know that, but how would I do it with things like death messages?
     
  8. Have you registered for PAPI?
    Examle class API name "PAPI" and go file main.java
    register
    new PAPI.register();
     
  9. I don’t need papi for what I need, I added it as an option. I just need something that says things about a death. Like killer, item used...
     
    • Winner Winner x 1
  10. So how do you like to create PlaceHolders, you want to use PlaceHoldersAPI or you are attempting to do local PlaceHolders to your plugin?
     
  11. @HarleySwtfus 's first comment very informative and pretty much explains the whole process,
    Basically if you want the actual code there it is:

    Main class:
    Code (Text):

    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;

    public class ExpansionTutorial extends JavaPlugin {

        @Override
        public void onEnable(){
            if( Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")){
                //Registering placeholder will be done here
            }
        }
    }
     
    Now you will need to create the PlaceholderExpansion class, just create new class into your project called "TutorialPlaceholder.class"
    and than just copy into the file this code below:
    Code (Text):

    import me.clip.placeholderapi.expansion.PlaceholderExpansion;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;

    public class TutorialPlaceholder extends PlaceholderExpansion {

        /*
        The identifier, shouldn't contain any _ or %
         */
        public String getIdentifier() {
            return "tutorial";
        }

        public String getPlugin() {
            return null;
        }


        /*
         The author of the Placeholder
         This cannot be null
         */
        public String getAuthor() {
            return "Banbeucmas";
        }

        /*
         Same with #getAuthor() but for versioon
         This cannot be null
         */

        public String getVersion() {
            return "SomeMagicalVersion";
        }

        /*
        Use this method to setup placeholders
        This is somewhat similar to EZPlaceholderhook
         */
        public String onPlaceholderRequest(Player player, String identifier) {
            /*
             %tutorial_onlines%
             Returns the number of online players
              */
            if(identifier.equalsIgnoreCase("onlines")){
                return String.valueOf(Bukkit.getOnlinePlayers().size());
            }
     
            /*
            Check if the player is online,
            You should do this before doing anything regarding players
             */
            if(player == null){
                return "";
            }
     
            /*
            %tutorial_name%
            Returns the player name
             */
            if(identifier.equalsIgnoreCase("name")){
                return player.getName();
            }
     
     
            return null;
        }
    }
     
    Break it down by your own it's pretty easy they already added notes to this class which
    let you understands it quickly and easy. :)

    And than just register the placeholder into your Main class, this way:
    Code (Text):

    new TutorialPlaceholder().register();
     
    Full Main class:
    Code (Text):

    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;

    public class ExpansionTutorial extends JavaPlugin {

        @Override
        public void onEnable(){
            if( Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")){
                new TutorialPlaceholder().register();
            }
        }
    }
     
    Original thread
     
  12. I kinda know that, just all I rlly need is placeholders about a death. Read my first message
     
  13. You can simply to put specific strings in messages to be replaced by data and then pass data from the events when you send them.
     
  14. I just don’t know what I would put for death things. I know for other things like player placeholders, I just need death placeholders.
     
  15. Well, the death event fires when a player dies. Grab data from that and pass it through the message.
     
  16. You want placeholder which tells you if a player alive or dead?
     
  17. I want a placeholder for death messages for about who was killed, what player killed them and what with
     
  18. Just few statements in PlayerDeathEvent event,
    such like
    Code (Text):

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent e) {
        Player player = (Player) e.getEntity();
        Player killer = (Player) e.getKiller();
        player.sendMessage(killer.getName() + " killed you, cause:" + player.getLastDamageCause().getEntityType());
        killer.sendMessage("You killed " + player.getName() +", cause:" + player.getLastDamageCause().getEntityType());
    }
     
    (Don't forget to if statement to get instance with the entities)
    Something like this i guess, then just store that values in HashMap and get it through the Expansion class of PlaceholderAPI.
     
  19. How would I make the item, killer and player into placeholders?