A tax funded religion
A tax funded religion

Reputation: 37

Java Minecraft Bukkit promote plugin

Here is my code:

package meg.zach.d;

import org.apache.logging.log4j.core.jmx.Server;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import net.md_5.bungee.api.ChatColor;

public class Main extends JavaPlugin {
    public void onEnable() {
        getLogger().info("Plugin Enabled");
    }

    public void onDisable() {

    }

    @SuppressWarnings("deprecation")
    public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
        Player p = (Player) sender;
        if (cmd.getName().equalsIgnoreCase("promote") && sender instanceof Player) {
            if (args[0] == "mod") {
                for (Player playerToPromote : Bukkit.getServer().getOnlinePlayers()) {
                    if (playerToPromote.getName().equalsIgnoreCase(args[1])) {
                        String modN = ChatColor.GRAY + "[Mod] ";
                        playerToPromote.setDisplayName(modN + playerToPromote.getDisplayName());
                        String modb = ChatColor.YELLOW + "has been promoted into a ";
                        String mod = ChatColor.GOLD + "Mod ";
                        Bukkit.getServer().broadcastMessage(playerToPromote + modb + mod);

                    }
                    else if(!(playerToPromote.isOnline())){
                        p.sendMessage(ChatColor.RED + "player not online");
                    }
                }

            }


        }
        return false;
    }

So my question is when I do /promote and mod and name it doesn't change the display name or anything, and also getOnlinePlayers is deprecated for some reason. Does anyone know how to solve this?

Upvotes: 0

Views: 83

Answers (2)

Stephen
Stephen

Reputation: 406

Alright, I have rewriten the code to look like this:

package meg.zach.d;

import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {

    // Got rid of onEnable(), simply sent a message to show the plugin was
    // enabled, but is done automatically by the server

    // Got rid of onDisable(), not necessary if empty

    Logger log = Logger.getLogger("Minecraft");

    @SuppressWarnings("deprecation")
    public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
        // The below checks if the sender is a player
        if (!(sender instanceof Player)) {
            log.info("error message : sender isnt player");
            return true;
        }
        // I can now safely cast sender to type Player
        Player p;
        if (cmd.getName().equalsIgnoreCase("commandnamehere")) {
            // Checking if there are enough arguments
            if (args.length != 1) {
                // Message to send when there isn't enough arguments
                p.sendMessage("Look! Not enough arguments!");
                return true;
            }
            if (args[0] == "lookaspecialthingy") {
                // Instead of looping through all the online players, I just try
                // to cast the player name to a Player, and check if the object
                // is null
                Player target = Bukkit.getPlayer(args[0]);
                if (target == null) {
                    // Player is offline
                    return true;
                }
                String modN = ChatColor.GRAY + "[Mod] ";
                target.setDisplayName(modN + target.getDisplayName());
                String modb = ChatColor.YELLOW + " has been promoted into a ";
                String mod = ChatColor.GOLD + "Mod!";
                Bukkit.getServer().broadcastMessage(target + modb + mod);

            }

        }
        return false;
    }
}

Keep in mind this code wasn't tested, but if you change it a bit, it should work just fine.

You would also need to have define the command in your plugin.yml. You can look up how to create a plugin.yml here: http://wiki.bukkit.org/Plugin_YAML

Upvotes: 1

LeWimbes
LeWimbes

Reputation: 507

I just wrote it with my had, so I don't know if everything is right, but if it isn't right it is nearly like this! So like isOfflinePlayer() could also be isOnlinePlayer()...

public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
    Player p = (Player) sender;
    if (cmd.getName().equalsIgnoreCase("promote") && sender instanceof Player) {
        if (args[0] == "mod") {
            Player playerToPromote = Bukkit.getPlayer(args[1]);
            if (!playerToPromote.isOfflinePlayer()) {
                    String modN = ChatColor.GRAY + "[Mod] ";
                    playerToPromote.setDisplayName(modN + playerToPromote.getDisplayName());
                    String modb = ChatColor.YELLOW + "has been promoted into a ";
                    String mod = ChatColor.GOLD + "Mod ";
                    Bukkit.getServer().broadcastMessage(playerToPromote + modb + mod);
                    playerToPromote.update();
                } else {
                    p.sendMessage(ChatColor.RED + "player not online");
                }

        }


    }
    return false;
}

You have to register your command in the plugin.yml and I don't know if the update() Method really exist... But with an Inventory it was like this. Sorry for my bad English and if it still doesn't work just command on my answer ;)

Upvotes: 0

Related Questions