Certain commands are not working & Other issues

Discussion in 'Plugin Development' started by Fishchunks, Jun 3, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Fishchunks

    Hi, I'm creating a plugin called 'Admin Rage' for my server, basically its just a group of commands which you can do when players are annoying you with certain, common issues and when I do /ch <username> I'm returned with the normal internal error response. Also, when I do no args I still get the error instead of the the Usage: bit.


    Code:
    if(commandLabel.equalsIgnoreCase("chill") || commandLabel.equalsIgnoreCase("ch")){
                    Player targetPlayer1 = player.getServer().getPlayer(args[0]);
                    Player targetPlayer2 = player.getServer().getPlayer(args[1]);
                    if(player.hasPermission("rq.ch") || player.isOp() || player.hasPermission("rq.*")){
                        if(args.length == 0 || args.length == 3){
                            player.sendMessage(ChatColor.DARK_RED + "Usage: /Chill <Username> <Username>");
                        }
                        if(args.length == 1){
                            Bukkit.broadcastMessage(ChatColor.GOLD + "[RageQuit] " + targetPlayer1.getName() + " needs to chill.");
                        }
                        if(args.length == 2){
                            Bukkit.broadcastMessage(ChatColor.GOLD + "[RageQuit] " + targetPlayer1.getName() + " and " + targetPlayer2.getName() + " need to chill.");
                        }
             
                    }else{
                        player.sendMessage(ChatColor.DARK_RED + "You do not have permission to use this command.");
                    }
                }



    Also, how can I make it so if they type a certain thing on a sign that it changes the colour for that bit? I don't need the whole code just the colour changing bit.

    EDIT:
    Could you help my with a chat event issue, here is the code
    [PLAYER LISTENER] http://pastebin.com/dNMia6es
    [MAIN] http://pastebin.com/xYmfPkh3
    When you type in either of those phrases it doesn't do anything.


    Thanks a lot,



    -Fishchunks.
     
  2. Offline

    Spartan_V23


    1) First things first. To add color to lets say a sign you use the code
    Code:
    .equals(ChatColor.COLOR + "TEXT HERE");
    
    2) The chat event issue is easy fix. Try doing something along the lines of
    Code:
    @EventHandler
    public void onPlayerChat(PlayerChatEvent event){
        Player player = event.getPlayer();
        String message = e.getMessage().toLowerCase();
             
          if(message.contains("planet minecraft") || message.contains("review your server") || message.contains("can i have op")){
                          player.setBanned(true);
                          Bukkit.broadcastMessage(player.getName() + " just asked for OP and as such got banned.");
                    }else{
                          return;
                    }
            }
    
    Something along those lines should fix that.

    3) As for your command issue. I think this may help you a bit more. Compare it to your code and change what needs changed. I am writing this very late at night so if there is some errors I apologize :).
    Code:
     
     
     
    /** Command Method **/
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    Player player = (Player) sender;
     
     
    if(cmd.getName().equalsIgnoreCase("chill") || cmd.getName().equalsIgnoreCase("ch")){
    chill(player, args);
    return true;
    }
     
    return false;
    }
     
    /** Execute Command Method **/
    public void chill(Player player){
     
    if(player.isOp() || player.hasPermission("rq.*") || player.hasPermission("rq.ch")){ // Checking permissions
    if(args.length > 0){ // Checking that arguments exist
    String playerName = args[0];
    if(!(playerName.equalsIgnoreCase("")) || playerName != null){ // Check if player name is null
    Player argsPlayer = getServer().getPlayer(playerName);
    if(argsPlayer != null){ // Checking to see if player is online.
    //Do your stuff here
    }else{
    player.sendMessage("Is player online?");
    }
    }
    }else{
    player.sendMessage(ChatColor.DARK_RED + "Usage: /Chill <Username>");
    }
    }
    }
    
    If you have any more questions, tag me in a post and ill come help ya some more ;)
     
    Fishchunks likes this.
  3. Offline

    Fishchunks

    Cheers man, I'll test this shortly! I wasn't 100% on the sign thing, so basically, if a person wrote [AM] it would convert that using the getLine(0) kinda thing?

    -Fish
     
  4. Offline

    Spartan_V23

    Exactly. My plugin caution signs uses this type of code. My code exactly is...
    Code:
    if(event.getLine(0).equalsIgnoreCase("[Caution]")){
    event.setLine(0, ChatColor.RED + "[Caution]");
    }
    
    So sounds like your on the right track :)

    Note: If you ever need to check the line on the sign in example an onPlayerMove event, make sure you check with the color code as the color code adds a symbol next to the text. Without the color code it will not "equal" the same text as on the sign.
     
    Fishchunks likes this.
  5. Offline

    Fishchunks

    I suppose I should ask this here instead of opening another thread, do you know how to I can make an external config file where people can edit the info && a file where I can write player info to?

    Thanks soooo much for all the help,

    -Fish
     
  6. Offline

    Spartan_V23


    Thats a big order lol. I will do my best to explain it lol. Without directly looking at your code and showing you what you need it will be hard to understand.

    1) For a file you can write player info to, you are going to need a file system. You will need to write a class, probs "FileIO.class" and this is where the writing to a txt will occur. The code below should work well for you as long as you understand it...
    Code:
     
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class FileIO {
    private File folder = new File("plugins"+File.separator+"PLUGINNAME"+File.separator);
    private File file = new File("plugins"+File.separator+"PLUGINNAME"+File.separator+"TEXTFILENAME.txt");
    @SuppressWarnings("unused")
    private JavaPlugin jp;
     
    public FileIO(JavaPlugin jp){
    this.jp = jp;
    if (!(file.exists())){
    folder.mkdirs();
    try {
    file.createNewFile();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
     
    public void saveFile(ArrayList<String> info){
    PrintWriter pw = null;
    try {
    pw = new PrintWriter(new BufferedWriter(new FileWriter(file, false)));
     
    for(String s: info){
    pw.println(s);
    }
     
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try {
    pw.close();
    } catch (Exception e){
    // nill
    }
     
    }
    }
     
    public ArrayList<String> array(){
    Scanner sc = null;
    ArrayList<String> arrPlayer = new ArrayList<String>();
    try {
    sc = new Scanner(file);
     
     
    while(sc.hasNext()){
    String temp = sc.nextLine();
    arrPlayer.add(temp);
     
    }
    return arrPlayer;
     
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
     
    } finally {
    close();
     
    }
    return null;
     
    }
     
    public FileConfiguration getConfig(){
    getConfig();
    return null;
    }
     
    }
    
    Note the FileConfiguration getConfig() method above.

    2) As for a config file you will need to create a "config.yml" and export it each time with your plugin kinda like your plugin.yml file. I recommend you make an initial startup method in public enable() that will check config versions etc. Example code below
    Code:
    public class PLUGINNAME extends JavaPlugin{
     
    private Logger log;
    private PluginDescriptionFile pdfFile;
    private FileIO fIO;
     
    public void onEnable(){
    pdfFile = getDescription();
    log = getServer().getLogger();
    fIO = new FileIO(this);
     
    init();
            log.info("[" + pdfFile.getName() + "]" + " Version " + pdfFile.getVersion() + " Is Enabled!");
    }
     
    public void init(){
    PluginDescriptionFile pdfFile = getDescription();
    File configLocation = new File("plugins"+File.separator+"PLUGINNAME"+File.separator+"config.yml");
    File oldConfiglocation = new File("plugins"+File.separator+"PLUGINNAME"+File.separator+"oldconfig.yml");
     
     
    if (!configLocation.exists()) {
    saveDefaultConfig();
    log.log(Level.INFO,"[" + pdfFile.getName() + "]" + " Default config.yml copied.");
    }
     
    if (!pdfFile.getVersion().equals(getConfig().getString("version"))) {
    log.log(Level.INFO,"[" + pdfFile.getName() + "]" + " Version of config doesn't match current plugin version.");
    getConfig().getDefaults();
    try {
    getConfig().save(oldConfiglocation);
    log.log(Level.INFO,"[" + pdfFile.getName() + "]" + " Config version: " + getConfig().getString("version"));
    log.log(Level.INFO,"[" + pdfFile.getName() + "]" + " Plugin version: " + pdfFile.getVersion());
    log.log(Level.INFO,"[" + pdfFile.getName() + "]" + " Back-up of old config successful.");
    log.log(Level.INFO,"[" + pdfFile.getName() + "]" + " New config set.");
    } catch (IOException ex) {
    log.log(Level.INFO,"[" + pdfFile.getName() + "]" + " Back-up of old config file failed. " + ex.getMessage());
    }
    configLocation.delete();
    saveDefaultConfig();
    }
     
    }
    
    3) As for the config file setup its self thats easy. Example below
    Code:
    version: 1.0
     
    config:
      toggle_this: true
    
    and to call on the "toggle_this" config you use the following code
    Code:
    if(getConfig().getBoolean("config.toggle_this")){
    // Do Something
    }
    


    Resource
    - http://wiki.bukkit.org/Introduction_to_the_New_Configuration
     
  7. Offline

    Fishchunks

    Thank sooooooooooooo much! I've known the internet config file since I started, just wondering how to do the external one......ONE LAST QUESTION (promise!) How can I get a user input (they do a command I.E /b hi hi hi) and instead of having to do if(args.length==2) so on and so on, could I store it in an array list?...I've tried a few ways just can't master it! Love you sooo much for all your help!
     
  8. Offline

    Spartan_V23


    I would probably do it in... hmm... Use an array list and use fileIO to write the string to the file for later use. That would probably be your best bet. In the example of fileio i gave you contains a savefile method used to write to the txt. Check it out. I am just too tired lol or ide give you some examples.
     
Thread Status:
Not open for further replies.

Share This Page