Placeholder does not work with Featherboard

Discussion in 'Spigot Plugin Development' started by Dramatically, May 6, 2017.

  1. Registering the Placeholder: it prints out "race"
    Code (Text):

       
            if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
                // placeholder code will register here \\
                new siaco.placeholderapi.Placeholders(this).hook();
                // placeholder code will end here \\

            } else {
                throw new RuntimeException("Could not find PlaceHolderAPI!! Plugin cannot operate without this!");
            }
            Writer.info("Loaded up placeholder: " + new siaco.placeholderapi.Placeholders(this).getPlaceholderName());
     
    PlaceHolder class
    Code (Text):

    SolarRaces plugin;

        public Placeholders(Plugin plugin) {
            super(plugin, "race");
            this.plugin = (SolarRaces) plugin;
        }

        @Override
        public String onPlaceholderRequest(Player player, String identifier) {
            // placeholder: %race%
            if (identifier.equals("name")) {
                return ChatListener.raceChat(player);
            }
            // always check if the player is null for placeholders related to the player!
            if (player == null) {
                return "Err 403";
            }
           
            return null;
        }

     
    i put {race}, {race_name}, %race%, %race_name% into featherboard it wont work? (on filezilla on a friends server)
     
  2. that "PlaceholderAPI" is for @clip 's projects, try the mvdwplaceholderapi https://www.spigotmc.org/resources/mvdwplaceholderapi.11182/
    Also you ask for the identifier "name"
    -> " if (identifier.equals("name")) {"

    that would mean your placeholder is %name%

    You may also have to use %placeholderapi_yourPluginName_name%
     
  3. Whats the API for mvdwplaceholderapi
     
  4. got the api done, going to test it now
     
  5. Does not work
    Code (Text):

    if (Bukkit.getPluginManager().isPluginEnabled("MVdWPlaceholderAPI")) {
                // placeholder code will register here \\
                PlaceholderAPI.registerPlaceholder(this, "race", new Placeholders(this));
                // placeholder code will end here \\

            } else {
                throw new RuntimeException("Could not find MVdWPlaceholderAPI!! Plugin cannot operate without this!");
            }
     
    Code (Text):


        SolarRaces plugin;

        public Placeholders(Plugin plugin) {
            this.plugin = (SolarRaces) plugin;
        }

        @Override
        public String onPlaceholderReplace(PlaceholderReplaceEvent arg0) {
           
            return ChatListener.raceChat(arg0.getPlayer());
        }
     
  6. @Phlox please help
     
  7. What do you mean with "does not work" ? Do you receive any errors? Or does it still return an none edited placeholder?
    Have you used {race} ?
    Maybe add some output messages for registering the placeholder and when the placeholderreplaceevent is beeing called.
    Also when your placeholders were added, there should be an console message for that.
     
  8. There is a console message ive tried {race} with featherboard and it just shows as its not a placeholder
     
  9. Could you provide your full source class where you use the placeholders with the Imports and your full Server log so i can go through that? If you don't wanna post that here just send me a pm
     
  10. I think there might be a rule against moving threads to PM (unless you just post the solution here, minus the code they sent you).
     
  11. There's no rule for helping someone on pm, its his opinion to decide of posting something here or somewhere else, the thread is still alive and when he found the solution, he's free to share it.
     
  12. I am fine revealing my code, If someone does copy it, it shows that they are pathetic and cannot make their own code.
    @Phloxz @ExpDev @MVdWSoftware
    Code (Text):

    package org.solar.races;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.craftbukkit.v1_8_R3.command.ColouredConsoleSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scheduler.BukkitTask;
    import org.solar.races.gui.GuiManager;
    import org.solar.races.listeners.ChatListener;
    import org.solar.races.listeners.RaceListener;
    import org.solar.races.listeners.SpawnListener;
    import org.solar.races.utils.PlayerFile;
    import org.spigotmc.SpigotConfig;

    import be.maximvdw.placeholderapi.PlaceholderAPI;
    import be.maximvdw.placeholderapi.PlaceholderReplaceEvent;
    import be.maximvdw.placeholderapi.PlaceholderReplacer;

    public class SolarRaces extends JavaPlugin implements Listener {

        public static class Writer {
            // Writer/Custom Logger Class
            public static void info(String message) {
                // Logger.info
                ColouredConsoleSender.getInstance().sendMessage("§7Races: §e" + message);
            }

            public static void warn(String message) {
                // Logger.warn
                ColouredConsoleSender.getInstance().sendMessage("§7Races: §4" + message);
            }

        }

        Map<Player, BukkitTask> effectTask = new HashMap<>();

        List<Player> teleporting = new ArrayList<>();

        Map<Player, BukkitTask> tasks = new HashMap<>();

        protected void addEffect(Player player) {
            if (new PlayerFile(player).getPlayerConfig().getString("race").equalsIgnoreCase("alien")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 60, 0, true, false), false);
            } else if (new PlayerFile(player).getPlayerConfig().getString("race").equalsIgnoreCase("astronaut")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 60, 0, true, false), false);
            } else if (new PlayerFile(player).getPlayerConfig().getString("race").equalsIgnoreCase("experiment")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 60, 0, true, false), false);
            } else {
                return;
            }
        }

        @EventHandler
        public void join(PlayerJoinEvent event) {
            for (final Player player : Bukkit.getOnlinePlayers()) {
                effectTask.put(player, new BukkitRunnable() {
                    @Override
                    public void run() {

                        if (new PlayerFile(player).getPlayerConfig().getString("race").equalsIgnoreCase("null")) {

                        } else {
                            addEffect(player);
                        }
                    }
                }.runTaskTimer(this, 0L, 0L));
            }
        }

        public static Location loc(Player p) {
            final FileConfiguration config = Bukkit.getPluginManager().getPlugin("SolarRaces").getConfig();
            final PlayerFile playerFile = new PlayerFile(p);
            if (playerFile.getPlayerConfig().getString("race") != null) {
                if (playerFile.getPlayerConfig().getString("race").equalsIgnoreCase("alien")) {
                    return new Location(Bukkit.getWorld(config.getString("spawns.1.world")), config.getDouble("spawns.1.x"),
                            config.getDouble("spawns.1.y"), config.getDouble("spawns.1.z"));
                } else if (playerFile.getPlayerConfig().getString("race").equalsIgnoreCase("astronaut")) {
                    return new Location(Bukkit.getWorld(config.getString("spawns.2.world")), config.getDouble("spawns.2.x"),
                            config.getDouble("spawns.2.y"), config.getDouble("spawns.2.z"));
                } else if (playerFile.getPlayerConfig().getString("race").equalsIgnoreCase("experiment")) {
                    return new Location(Bukkit.getWorld(config.getString("spawns.3.world")), config.getDouble("spawns.3.x"),
                            config.getDouble("spawns.3.y"), config.getDouble("spawns.3.z"));
                } else {
                    Writer.warn("String race is not alien or astronaut or experiment but isnt null?");
                    return null;
                }
            } else {
                Writer.warn("String race is null");
            }
            return null;
        }

        @EventHandler
        public void move(PlayerMoveEvent event) {
            if (event.getFrom().getBlockX() != event.getTo().getBlockX()) {
                if (teleporting.contains(event.getPlayer())) {
                    teleporting.remove(event.getPlayer());
                    tasks.remove(event.getPlayer());
                    event.getPlayer().sendMessage("§e§l(!) §7Teleportation cancelled, you moved");
                }
            }
        }

        // Command -> /race change, /race setspawn <race>
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            // start of command /race
            if (command.getName().equalsIgnoreCase("race")) {
                final String helpMessage = "§e§l(!) §7Please try §e§n/race change§7 or §e§n/race setspawn <race>";

                if (sender.hasPermission("*") || sender.isOp()) {

                    if (args.length == 0 || args.length < 1) {

                        sender.sendMessage(helpMessage);

                    } else if (args.length > 0) {

                        if (args.length == 1) {

                            if (args[0].equalsIgnoreCase("change")) {

                                try {
                                    final Player player = (Player) sender;
                                    GuiManager.openRace(player);
                                } catch (final Exception ex) {
                                    sender.sendMessage("Something went wrong...");
                                    ex.printStackTrace();
                                }
                            } else if (args[0].equalsIgnoreCase("setspawn")) {
                                sender.sendMessage(helpMessage);
                            } else {
                                sender.sendMessage(helpMessage);
                            }

                        } else if (args.length == 2) {
                            if (args[0].equalsIgnoreCase("setspawn")) {
                                if (args[1].equalsIgnoreCase("1") || args[1].equalsIgnoreCase("2")
                                        || args[1].equalsIgnoreCase("3")) {
                                    if (args[1].equalsIgnoreCase("1")) {
                                        final ConfigurationSection s = getConfig().createSection("spawns.1");
                                        try {
                                            final Player player = (Player) sender;
                                            s.set("world", player.getWorld().getName());
                                            s.set("x", player.getLocation().getX());
                                            s.set("y", player.getLocation().getY());
                                            s.set("z", player.getLocation().getZ());
                                            s.set("yaw", player.getLocation().getYaw());
                                            s.set("pitch", player.getLocation().getPitch());
                                        } catch (final Exception ex) {
                                            sender.sendMessage("Something went wrong...");
                                            ex.printStackTrace();
                                        }
                                        saveConfig();
                                    } else if (args[1].equalsIgnoreCase("2")) {
                                        final ConfigurationSection s = getConfig().createSection("spawns.2");
                                        try {
                                            final Player player = (Player) sender;
                                            s.set("world", player.getWorld().getName());
                                            s.set("x", player.getLocation().getX());
                                            s.set("y", player.getLocation().getY());
                                            s.set("z", player.getLocation().getZ());
                                            s.set("yaw", player.getLocation().getYaw());
                                            s.set("pitch", player.getLocation().getPitch());
                                        } catch (final Exception ex) {
                                            sender.sendMessage("Something went wrong...");
                                            ex.printStackTrace();
                                        }
                                        saveConfig();
                                    } else if (args[1].equalsIgnoreCase("3")) {
                                        final ConfigurationSection s1 = getConfig().createSection("spawns.3");
                                        try {
                                            final Player player = (Player) sender;
                                            s1.set("world", player.getWorld().getName());
                                            s1.set("x", player.getLocation().getX());
                                            s1.set("y", player.getLocation().getY());
                                            s1.set("z", player.getLocation().getZ());
                                            s1.set("yaw", player.getLocation().getYaw());
                                            s1.set("pitch", player.getLocation().getPitch());
                                        } catch (final Exception ex) {
                                            sender.sendMessage("Something went wrong...");
                                            ex.printStackTrace();
                                        }
                                        saveConfig();
                                    } else {
                                        sender.sendMessage(helpMessage);
                                    }
                                } else {
                                    sender.sendMessage(
                                            "Try adding instead of <race> 1 = Alien, 2 = Astronaut, 3 = Experiment");
                                }
                            } else {
                                sender.sendMessage(helpMessage);
                            }
                        }

                    } else {

                    }

                } else {
                    sender.sendMessage(SpigotConfig.unknownCommandMessage);

                }
                // end of command /race
                // start of /spawn

            } else if (command.getName().equalsIgnoreCase("spawn")) {
                if (args.length == 0) {

                    sender.sendMessage("§e§l(!) §7You will be teleported in 5 seconds");
                    try {
                        teleporting.add((Player) sender);
                    } catch (final Exception ex) {
                        System.err.println("Sorry this sender " + sender + " could not be cast to entity teleport map");
                    }

                    try {
                        tasks.put((Player) sender, new BukkitRunnable() {
                            @Override
                            public void run() {
                                try {

                                    final Player player = (Player) sender;
                                    if (teleporting.contains(player)) {
                                        player.teleport(loc(player));
                                        player.sendMessage("§e§l(!) §7You have been teleported to §e§nspawn!");
                                        teleporting.remove(player);
                                    } else {

                                    }
                                } catch (final Exception ex) {
                                    sender.sendMessage("Something went wrong...");
                                    try {
                                        teleporting.remove(sender);
                                    } catch (final Exception ex2) {

                                    }
                                }

                            }
                        }.runTaskLater(this, 20 * 5L));
                    } catch (final Exception ex) {
                        sender.sendMessage("Sorry, something went wrong...");
                    }

                } else if (args.length == 1) {

                    sender.sendMessage("§e§l(!) §7Issued teleport request for §e§n" + args[0] + "§7 to spawn!");

                    try {
                        final Player target = Bukkit.getPlayer(args[0]);
                        target.teleport(loc(target));
                        target.sendMessage("§e§l(!) §7You have been teleported to §e§nspawn!");
                        sender.sendMessage("§e§l(!) §7Teleported §e§n" + args[0] + "§7 to spawn!");
                    } catch (final Exception ex) {
                        sender.sendMessage("Something went wrong...");
                    }

                } else {
                    return true;
                }
            }
            // end of /spanw
            return false;
        }

        @Override
        public void onDisable() {
            // Plugin Disable
            Writer.info("Disabled Plugin");

        }

        @Override
        public void onEnable() {
       
       
            // Plugin Enable
            Writer.info("Enabled Plugin");
            saveDefaultConfig();
            getConfig().options().copyDefaults(true);
            getCommand("race").setExecutor(this);
            getCommand("spawn").setExecutor(this);
       
       
            if (Bukkit.getPluginManager().isPluginEnabled("MVdWPlaceholderAPI")) {
                // placeholder code will register here \\
                PlaceholderAPI.registerPlaceholder(this, "race", new PlaceholderReplacer() {
               
                    @Override
                    public String onPlaceholderReplace(PlaceholderReplaceEvent paramPlaceholderReplaceEvent) {
                        Writer.info("Registered: " + paramPlaceholderReplaceEvent.getPlaceholder());
                        return new PlayerFile(paramPlaceholderReplaceEvent.getPlayer()).getPlayerConfig().getString("race");
                    }
                });
                // placeholder code will end here \\

            } else {
                throw new RuntimeException("Could not find MVdWPlaceholderAPI!! Plugin cannot operate without this!");
            }

            Bukkit.getPluginManager().registerEvents(this, this);
            Bukkit.getPluginManager().registerEvents(new SpawnListener(), this);
            Bukkit.getPluginManager().registerEvents(new ChatListener(), this);
            Bukkit.getPluginManager().registerEvents(new RaceListener(), this);

            for (final Player player : Bukkit.getOnlinePlayers()) {
                effectTask.put(player, new BukkitRunnable() {
                    @Override
                    public void run() {

                        if (new PlayerFile(player).getPlayerConfig().getString("race").equalsIgnoreCase("null")) {

                        } else {
                            addEffect(player);

                        }
                    }
                }.runTaskTimer(this, 60L, 0L));
            }

        }

        @Override
        public void onLoad() {
            // Plugin Load
            Writer.info("Loaded Plugin");
        }

        @EventHandler
        public void quit(PlayerQuitEvent event) {
            for (final Player player : Bukkit.getOnlinePlayers()) {
                effectTask.put(player, new BukkitRunnable() {
                    @Override
                    public void run() {

                        if (new PlayerFile(player).getPlayerConfig().getString("race").equalsIgnoreCase("null")) {

                        } else {
                            addEffect(player);

                        }
                    }
                }.runTaskTimer(this, 60L, 0L));
            }
        }

    }

     
    MvdwPlaceholder plugin registers it in console output but the code i wrote to debug the registering of the placeholder is not being printed out, did i do something wrong?
     
    #12 Dramatically, May 7, 2017
    Last edited: May 7, 2017