Results 1 to 1 of 1
  1. #1
    Bot Bot's Avatar
    Join Date
    Aug 2011
    Posts
    2,671
    Points
    7,720
         User Info     Contact     Gamer ID
    Join Date
    Aug 2011
    Posts
    2,671
    Points
    7,720

    Default Minecraft Bukkit Plugin Tutorial - Tutorial 3 - Test Plugin

    Test Plugin

    Hopefully you've followed the previous tutorials and so you should have a blank plugin which you have yet to code. So we now need to edit the class .java file that was created.

    onEnable and onDisable

    So first thing to do is to utilise the onEnable and onDisable code. So within the { and } for onEnable add Bukkit.getServer().getLogger().info("Test Plugin: Enabled!");, repeat this for onDisable but change the command so we have Bukkit.getServer().getLogger().info("Test Plugin: Disabled!");. Your code should look like this:

    Code:
    	public void onEnable(){
    		Bukkit.getServer().getLogger().info("Test Plugin: Enabled!");
    	}
    	
    	public void onDisable(){
    		Bukkit.getServer().getLogger().info("Test Plugin: Disabled!");
    	}
    All the text within the speech marks can be changed to whatever you like, this command simply displays either message within the console when you start the plugin (so when the server starts up) or whenever you end the plugin.

    The Plugin Tasks

    Now we need to program our plugin to do something. So we need to add our code:

    [CODE]
    public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {

    if(!(sender instanceof Player)){
    sender.sendMessage(ChatColor.RED + "The console ran the test command!");
    return true;
    }

    Player player = (Player) sender;

    if (cmd.getName().equalsIgnoreCase("testme")) {
    player.sendMessage(ChatColor.GOLD + "You ran the test command!");
    }
    return true;
    }
    [CODE]

    So this may seem a little complicated and you may not be able to understand it but I will go through it all now.

    Your first line is a Boolean, this is simply a data type which has two variables: true or false. This first line what we want to do and at the end of the process we will get either a value of true or false. The command is started and we have some simple declarations. The important part of the code is inside the this Boolean.

    Our first if function simply checks if the sender (who ever activated the plugin via the assigned command) is not a player, if the sender is not a player then sender.sendMessage(ChatColor.RED + "The console ran the test command!"); is executed. All this does is send the sender, in this case the console, a message in the colour red saying "The console ran the test command!". And then we satisfy the Boolean by using return true;, this tells the program the actions were successful. In later plugins we can add return false; and issue messages which inform the user of how to correctly use the plugin.

    So if the command is used within the console the code ends there, however if the command was sent from a player then we skip over that part of the code and continue on. Now we know the sender is a player we put Player player = (Player) sender;, this declares that our variable player is a player, and it's value is equal to the player who sent the command. So next we check if the command has been used, our if function continually checks for when the user tries to use our command, in this case our command is testme and so if the user enters /testme our code inside will run. The cmd.getName() part watches for when our command is used and equalsIgnoreCase("testme") tells us that the use of capitalisation will not matter and we are looking for /testme.

    Once the command is used the code inside player.sendMessage(ChatColor.GOLD + "You ran the test command!"); is run. This sends our player, who used the command, a message in gold saying "You ran the test command!". And then finally we, again, satisfy the Boolean by declaring return true;.

    The plugin.yml

    We now have a basic plugin almost coded, to finish we need to declare our command, and we can add a description. So we need to add the following code to the end of the plugin.yml file. All commands are listed under commands: however in this case we are only using the one command, testme: and conventionally we leave a two space gap for each indent as sown below.

    Code:
    commands:
      testme:
         description: Plugin Test.
    If we needed multiple commands the indentation returns and we do not redeclare commands:, so if we wanted another command testme2: it would look something like this:

    Code:
    commands:
      testme:
         description: Plugin Test.
      testme2:
         description: Plugin Test 2.
    Compile and Test

    That is now all the necessary code for our plugin to operate and run correctly on our server, so now you just need to compile the plugin into the plugins folder of your Minecraft server and then run your server. Now you can test the testme command in the console and within Minecraft.

    /////////////////////////////////////////////////////////////////////////////////////////////


    0 Not allowed! Not allowed!
    Last edited by Bot; 06-14-2013 at 12:15 AM.








Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -10. The time now is 08:21 PM.
Powered by vBulletin®
Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.