[Help] Adding somethings to Plugin

Discussion in 'Plugin Development' started by FallenYouth, Dec 8, 2013.

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

    FallenYouth

    Hello everyone!

    I am trying to add a toggling command to my plugin FlashlightPlus but not really sure how.

    Another thing I am trying to accomplish is when the Flashlight is on mobs will not spawn in the configured distance(In Blocks) away.

    FlashlightPlus.java
    Code:java
    1. package com.hybrah;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8. import org.bukkit.potion.PotionEffect;
    9. import org.bukkit.potion.PotionEffectType;
    10.  
    11.  
    12. public class FlashlightPlus extends JavaPlugin {
    13.  
    14. public void onEnable() {
    15. this.getConfig().options().copyDefaults(true);
    16. getServer().getPluginManager().disablePlugin(this);
    17. return;
    18. }
    19. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    20.  
    21. if(cmd.getName().equalsIgnoreCase("Flashlight") || cmd.getName().equalsIgnoreCase("flash") || cmd.getName().equalsIgnoreCase("fl")) {
    22.  
    23. if (!(sender instanceof Player)) {
    24. sender.sendMessage(ChatColor.RED + "Must be a player to exucute this command!");
    25. return true;
    26. }
    27.  
    28. if (!sender.hasPermission("flashlight.use")) {
    29. sender.sendMessage(ChatColor.RED + "You do not have permission to perform this command!");
    30. return true;
    31. }
    32.  
    33. if(args.length <= 0){
    34.  
    35. sender.sendMessage(ChatColor.GREEN + "/flashlight on - Turns Flashlight ON!");
    36. sender.sendMessage(ChatColor.GREEN + "/flashlight off - Turns Flashlight OFF!");
    37.  
    38. return true;
    39. }
    40.  
    41. if (args.length == 1) {
    42.  
    43. if (sender instanceof Player) {
    44. Player player = (Player) sender;
    45.  
    46. if(args[0].equalsIgnoreCase("on")) {
    47. player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0));
    48. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("FlashlightOnMsg")));
    49. return true;
    50. }
    51. if(args[0].equalsIgnoreCase("off")) {
    52. player.removePotionEffect(PotionEffectType.NIGHT_VISION);
    53. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("FlashlightOffMsg")));
    54. }
    55. }
    56. }
    57. }
    58. return true;
    59. }
    60. }
    61.  


    I know I’m going to need to remove some code in-order to have the toggling command feature.

    Thanks in advance,

    FallenYouth
     
  2. FallenYouth
    The toggling is easy. Replace lines 46-54 with
    Code:java
    1. if(args[0].equalsIgnoreCase("on")){
    2. player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0));
    3. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("FlashlightOnMsg")));
    4. flashlightOn = true;
    5. returntrue;
    6. }
    7. if(args[0].equalsIgnoreCase("off")){
    8. player.removePotionEffect(PotionEffectType.NIGHT_VISION);
    9. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("FlashlightOffMsg")));
    10. flashlightOn = false;
    11. return true; //You forgot to add this
    12. }
    13. if(args[0].equalsIgnoreCase("toggle")) {
    14. if(flashlightOn){
    15. player.removePotionEffect(PotionEffectType.NIGHT_VISION);
    16. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("FlashlightOffMsg")));
    17. flashlightOn = false;
    18. return true;
    19. } else {
    20. player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0));
    21. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("FlashlightOnMsg")));
    22. flashlightOn = true;
    23. return true;
    24. }
    25. }
    and finally add on line 13:
    Code:java
    1. boolean flashlightOn = false;


    FallenYouth
    For the spawn radius, take a look at CreatureSpawnEvent and check that the location is further than your range, and if not, cancel it. If you want your range to be a sphere rather than a cube, make a vector between the entitylocation and the player location, and then check the length against your range

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page