Need some help with commands please.

Discussion in 'Plugin Development' started by Fallen_Advent, Aug 30, 2011.

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

    Fallen_Advent

    Ok guys, I watched all of Dinnerbone's tutorials about the Red-Carpet tutorial and I have a very small idea on how to run command and arguments with those commands.

    What I am trying to achieve is to have a plugin change the material type that the player uses as a "carpet" using a command like this, "/<command> <material type> <radius>" to essentially allow the player to build walls or roads using the same concept as the redcarpet design without it going away after a period of time or within a distance of the player. This would also allow them to make bridges or floors for buildings.

    I already know how to make the blocks not change back ( Just ignore tutorial 3 lol )

    So i guess what I am asking, is how do I allow the player to specify what value <material type> and <radius> would be.

    Wow does no one help new developers on this site, 'cause this is the 3rd question I have posted in this forum (Not all of the questions are the same) and no one seems to be nice enough to lend a new dev a hand.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  2. Offline

    Jogy34

    It would be something like this
    Code:
     public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)     {
     if( command.getName( ).equalsIgnoreCase( "Make" ) )\\or whatever you want the command to be
     {
    if ( args.length == 2 )
     {
      return true;
     }
    \\what you want to check the args to be the the rest of the code if they are correct
    }
    }
    
    I didn't actually check if that works and it's just off the top of my head
     
  3. Offline

    Fallen_Advent

    Wow, Thank you, I will try that out, Glad to see someone on here is willing to help, Thanks tons man (that wasn't sarcastic either, I really am grateful)
     
  4. Hmm yeah...I don't do anything to help new devs do I....http://wiki.bukkit.org/HUGE_Plugin_Tutorial
     
  5. Offline

    Fallen_Advent

    @Adamki11s I know about the huge plugin tutorial, But sometimes I need things more on a case-by-case basis. I do thank you for that tutorial, It helps out a lot.

    --Edit-- I am sorry about the abrupt rudeness of my second post. I was tired from work and I let my impatience get a hold of me, So I do apologize.
     
  6. It's alright; I understand the frustration from when I was a new dev but when I joined there was hardly any tutorials or that matter experienced devs :p
     
  7. Offline

    Fallen_Advent

    @Adamki11s Ya, Well if you have the time to answer a quick question for me, I am trying to make it where someone types /<command> Stone <radius> it recognizes the second argument as a material and sets the block under the player to that material
     
  8. Inside your onCommand function

    Code:java
    1.  
    2. onCommand(CommandSender sender, Command cmd, String label, String[] args){
    3. if(label.equalsIgnoreCase("command")){
    4. if(args.length == 2){
    5. if(!(sender instanceof Player)){
    6. return false;//no console commands
    7. }
    8. Player p = (Player)sender;//cast to a player object
    9. Material m = Material.getMaterial(args[0]);
    10. if(m == null){ return false; } //Invalid material
    11. //for the radius you should look at using my extras library because those functions might be complicated to a new dev.
    12. return true;
    13. }
    14. }
    15. return false;
    16. }
     
  9. Offline

    Fallen_Advent

    @Adamki11s Okay so forgive my utter noobness, when doing a command the args would be like this
    /<command> arg[0] arg[1] arg[2] and so on, correct?
     
  10. correct
     
  11. Offline

    Fallen_Advent

    @Adamki11s Cool, Thanks for all the help man.
     
  12. No problem
     
  13. Offline

    Fallen_Advent

    @Adamki11s How did you put your code into the Java format on the forum, All i can get is PHP and HTML

    --Edit-- Nevermind, I figured it out ^_^

    @Adamki11s

    Ok so a bit of revision, I took out the radius argument. And here is what I have so far. Please help me out if you have the time cause at this point I have no idea what to do.

    FallenBlocks.java
    Show Spoiler

    Code:java
    1.  
    2. package me.fallenadvent.plugins.fallenblocks;
    3.  
    4. import java.util.HashSet;
    5. import java.util.Set;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.Event.Priority;
    13. import org.bukkit.event.Event.Type;
    14. import org.bukkit.event.player.PlayerListener;
    15. import org.bukkit.plugin.PluginDescriptionFile;
    16. import org.bukkit.plugin.PluginManager;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class FallenBlocks extends JavaPlugin {
    20. private PluginDescriptionFile info;
    21. private PluginManager manager;
    22. private PlayerListener playerListener = new FBPlayerListener(this);
    23. private Set<Player> FbBuilder = new HashSet<Player>();
    24. public void onDisable() {
    25. info = getDescription();
    26. System.out.println("[" + info.getName() + " " + info.getVersion() + "] has been disabled.");
    27. }
    28. public void onEnable() {
    29. info = getDescription();
    30. getServer().getPluginManager().registerEvent(Type.PLAYER_MOVE, playerListener, Priority.Highest, this);
    31. System.out.println("[" + info.getName() + " " + info.getVersion() + "] has been enabled.");
    32. getCommand("fallenbuild").setExecutor(new CommandExecutor() {
    33.  
    34. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    35. if(label.equalsIgnoreCase("Fallenbuild")){
    36. if(args.length == 1){
    37. if(!(sender instanceof Player)){
    38. sender.sendMessage(ChatColor.RED + "That command must be used by a player.");
    39. return false;//No Console Commands
    40. } else {
    41. Player player = (Player)sender;//Cast to a player object
    42. Material materialtype = Material.getMaterial(args[0]);
    43. if (player.hasPermission("fallenbuild.build")){
    44. setFbBuilder(player, !hasFbBuilder(player));
    45. } else {
    46. player.sendMessage(ChatColor.RED + "You aren't special enough for a red carpet.");
    47. }
    48. if (materialtype == null){
    49. sender.sendMessage(ChatColor.RED + "That is not a valid material type.");
    50. return false; }//Invalid material
    51. return true;
    52. }
    53. }
    54. }
    55. return false;
    56. }
    57. });
    58. }
    59. public boolean hasFbBuilder(Player player) {
    60. return FbBuilder.contains(player);
    61. }
    62. public void setFbBuilder(Player player, boolean enabled) {
    63. if (enabled) {
    64. FbBuilder.add(player);
    65. } else {
    66. FbBuilder.remove(player);
    67. }
    68. }
    69. }
    70.  




    FBPlayerListener.java
    Show Spoiler

    Code:java
    1.  
    2.  
    3. package me.fallenadvent.plugins.fallenblocks;
    4.  
    5.  
    6. import org.bukkit.block.Block;
    7. import org.bukkit.block.BlockFace;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.player.PlayerListener;
    10. import org.bukkit.event.player.PlayerMoveEvent;
    11.  
    12. public class FBPlayerListener extends PlayerListener{
    13. private final FallenBlocks plugin;
    14. public FBPlayerListener(FallenBlocks plugin) {
    15. this.plugin = plugin;
    16. }
    17.  
    18. @Override
    19. public void onPlayerMove(PlayerMoveEvent event) {
    20. Player player = event.getPlayer();
    21. Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
    22. if (!player.hasPermission("fallenbuild.build")) {
    23. return;
    24. }
    25. if (!plugin.hasFbBuilder(player)) {
    26. return;
    27. }
    28. }
    29.  
    30. }
    31.  



    plugin.yml
    Show Spoiler

    Code:YML
    1.  
    2. author: Fallen Advent
    3. database: false
    4. description: Simple but flexible underfoot building for your server's players.
    5. generator: [URL]http://dinnerbone.com/minecraft/tools/pluginator/[/URL]
    6. main: me.fallenadvent.plugins.fallenblocks.FallenBlocks
    7. name: Fallen Blocks
    8. startup: postworld
    9. version: '1.0'
    10. permissions:
    11. fallenbuild.build:
    12. default: op
    13. description: Allows the player to use the /fallenbuild command.
    14. commands:
    15. fallenbuild:
    16. usage: | /<command> [material]
    17. description: Allows the player to build using the block directly underfoot, Similar to a magic carpet that is persistant.
    18.  
    19.  



    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  14. Code:
    [syntax=java] your code [/syntax]
    I'd recommend you hook into my extras library and use the terrain methods.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  15. Offline

    Fallen_Advent

    @Adamki11s

    Ok, So I believe I hooked into it right, I am just having trouble allowing the onPlayerMove to grab the values from the arguments so that I can get the onPlayerMove event to change the values correctly.

    FallenBlocks.java

    Show Spoiler

    Code:java
    1.  
    2.  
    3. package me.fallenadvent.plugins.fallenblocks;
    4.  
    5.  
    6. import couk.Adamki11s.Extras.Extras.Extras;
    7. import java.util.HashSet;
    8. import java.util.Set;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Material;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandExecutor;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.Event.Priority;
    16. import org.bukkit.event.Event.Type;
    17. import org.bukkit.event.player.PlayerListener;
    18. import org.bukkit.plugin.PluginDescriptionFile;
    19. import org.bukkit.plugin.PluginManager;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21.  
    22. public class FallenBlocks extends JavaPlugin {
    23. private PluginDescriptionFile info;
    24. private PluginManager manager;
    25. private PlayerListener playerListener = new FBPlayerListener(this);
    26. private Set<Player> FbBuilder = new HashSet<Player>();
    27.  
    28. @Override
    29. public void onDisable() {
    30. info = getDescription();
    31. System.out.println("[" + info.getName() + " " + info.getVersion() + "] has been disabled.");
    32. }
    33. @Override
    34. public void onEnable() {
    35. Extras ex = new Extras("FallenBlocks");
    36. info = getDescription();
    37. getServer().getPluginManager().registerEvent(Type.PLAYER_MOVE, playerListener, Priority.Highest, this);
    38. System.out.println("[" + info.getName() + " " + info.getVersion() + "] has been enabled.");
    39. getCommand("fallenbuild").setExecutor(new CommandExecutor() {
    40.  
    41. @Override
    42. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    43. if(label.equalsIgnoreCase("Fallenbuild")){
    44. if(args.length == 1){
    45. if(!(sender instanceof Player)){
    46. sender.sendMessage(ChatColor.RED + "That command must be used by a player.");
    47. return false;//No Console Commands
    48. } else {
    49. Player player = (Player)sender;//Cast to a player object
    50. Material materialtype = Material.getMaterial(args[0]);
    51. Integer radius = Integer.getInteger(args[1]);
    52. if (player.hasPermission("fallenbuild.build")){
    53. setFbBuilder(player, !hasFbBuilder(player));
    54. } else {
    55. player.sendMessage(ChatColor.RED + "You aren't special enough for a red carpet.");
    56. }
    57. if (materialtype == null){
    58. sender.sendMessage(ChatColor.RED + "That is not a valid material type.");
    59. return false; }//Invalid material
    60. if (radius == null){
    61. sender.sendMessage(ChatColor.RED + "That is not a valid size.");
    62. return false;}//Invalid material
    63. return true;
    64. }
    65. }
    66. }
    67. return false;
    68. }
    69. });
    70. }
    71. public boolean hasFbBuilder(Player player) {
    72. return FbBuilder.contains(player);
    73. }
    74. public void setFbBuilder(Player player, boolean enabled) {
    75. if (enabled) {
    76. FbBuilder.add(player);
    77. } else {
    78. FbBuilder.remove(player);
    79. }
    80. }
    81. }
    82.  



    FBPlayerListener.java

    Show Spoiler

    Code:java
    1.  
    2.  
    3. package me.fallenadvent.plugins.fallenblocks;
    4.  
    5. import org.bukkit.block.Block;
    6. import org.bukkit.block.BlockFace;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.player.PlayerListener;
    9. import org.bukkit.event.player.PlayerMoveEvent;
    10.  
    11. public class FBPlayerListener extends PlayerListener{
    12. private final FallenBlocks plugin;
    13. public FBPlayerListener(FallenBlocks plugin) {
    14. this.plugin = plugin;
    15. }
    16.  
    17. @Override
    18. public void onPlayerMove(PlayerMoveEvent event) {
    19. Player player = event.getPlayer();
    20. Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
    21. if (!player.hasPermission("fallenbuild.build")) {
    22. return;
    23. }
    24. if (!plugin.hasFbBuilder(player)) {
    25. return;
    26. }
    27. }
    28.  
    29. }
    30.  
    31.  


    plugin.yml

    Show Spoiler

    Code:yml
    1.  
    2. author: Fallen Advent
    3. database: false
    4. description: Simple but flexible underfoot building for your server's players.
    5. generator: [URL]http://dinnerbone.com/minecraft/tools/pluginator/[/URL]
    6. main: me.fallenadvent.plugins.fallenblocks.FallenBlocks
    7. name: Fallen Blocks
    8. startup: postworld
    9. version: '1.0'
    10. permissions:
    11. fallenbuild.build:
    12. default: op
    13. description: Allows the player to use the /fallenbuild command.
    14. commands:
    15. fallenbuild:
    16. usage: | /<command> [material] [radius]
    17. description: Allows the player to build using the block directly underfoot, Similar to a magic carpet that is persistant.
    18.  
    19.  

     
Thread Status:
Not open for further replies.

Share This Page