1.15.2 Own plugin api Prefix

Discussion in 'Spigot Plugin Development' started by ChisleLP, Mar 27, 2020 at 1:22 AM.

  1. Hey, im kinda still fresh to API.
    Just found out that API is a nice things to reduce my Code for some my Plugins, and i wounder if there was a possible to set every own plugin prefix?
    I just did this ways but i think it kinda dumb because i have to put everywhere "this" as plugins, is there maybe a another ways?
    This is the Main one.
    Code (Java):
    package de.freddo.testapi.main;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.HashMap;

    public class testapi extends JavaPlugin {

        private HashMap<String, String> prefix = new HashMap<>();

        @Override
        public void onEnable() {
            System.out.println("loaded");
        }

        public boolean setPrefix(Plugin plugin, String prefix) {
            this.prefix.put(plugin.getName(), prefix);
            return true;
        }

        public String getPrefix(Plugin plugin) {
            return this.prefix.get(plugin.getName());
        }

        public boolean sendPlayer(Plugin plugin, Player player, String message) {
            player.sendMessage(getPrefix(plugin)+" "+message);
            return true;
        }

        public boolean sendConosole(Plugin plugin, String message) {
            Bukkit.getServer().getConsoleSender().sendMessage(getPrefix(plugin) + " " + message);
            return true;
        }
    }
    On other Plugins:
    Code (Java):

    package de.freddo.test1.main;

    import de.freddo.testapi.main.testapi;
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
        private testapi api = (testapi) Bukkit.getServer().getPluginManager().getPlugin("testapi");

        @Override
        public void onEnable() {
            api.setPrefix(this,"ยง6[Test1]");
            api.sendConosole(this,"Hallo wie geht es ihnen?");
        }
    }
    my question were, it possible to change/remove somehow the "this"/Plugin?
    Because i want to set on other plugin other prefix. so i have multiple prefix.

    Thanks,

    ChisleLP.
     
  2. For console logs the prefix defined in your plugin.yml is used, by default it's the same as the plugin's name, but you can override it. e.g.:
    Code (YAML):
    name: MyPlugin
    version
    : 1.0
    main
    : com.example.MyPlugin
    prefix
    : MyPrefix
    For broadcasts or per-player messages, just write extra methods that take the prefix as an argument.