All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bukkit.command.defaults.DifficultyCommand Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
package org.bukkit.command.defaults;

import com.google.common.collect.ImmutableList;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

@Deprecated
public class DifficultyCommand extends VanillaCommand {
  private static final List DIFFICULTY_NAMES = ImmutableList.of("peaceful", "easy", "normal", "hard");

  public DifficultyCommand() {
    super("difficulty");
    this.description = "Sets the game difficulty";
    this.usageMessage = "/difficulty  ";
    this.setPermission("bukkit.command.difficulty");
  }

  @Override
  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
    if (!testPermission(sender)) return true;
    if (args.length != 1 || args[0].length() == 0) {
      sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
      return false;
    }

    Difficulty difficulty = Difficulty.getByValue(getDifficultyForString(sender, args[0]));

    if (Bukkit.isHardcore()) {
      difficulty = Difficulty.HARD;
    }

    Bukkit.getWorlds().get(0).setDifficulty(difficulty);

    int levelCount = 1;
    if (Bukkit.getAllowNether()) {
      Bukkit.getWorlds().get(levelCount).setDifficulty(difficulty);
      levelCount++;
    }

    if (Bukkit.getAllowEnd()) {
      Bukkit.getWorlds().get(levelCount).setDifficulty(difficulty);
    }

    Command.broadcastCommandMessage(sender, "Set difficulty to " + difficulty.toString());
    return true;
  }

  protected int getDifficultyForString(CommandSender sender, String name) {
    if (name.equalsIgnoreCase("peaceful") || name.equalsIgnoreCase("p")) {
      return 0;
    } else if (name.equalsIgnoreCase("easy") || name.equalsIgnoreCase("e")) {
      return 1;
    } else if (name.equalsIgnoreCase("normal") || name.equalsIgnoreCase("n")) {
      return 2;
    } else if (name.equalsIgnoreCase("hard") || name.equalsIgnoreCase("h")) {
      return 3;
    } else {
      return getInteger(sender, name, 0, 3);
    }
  }

  @Override
  public List tabComplete(CommandSender sender, String alias, String[] args) {
    Validate.notNull(sender, "Sender cannot be null");
    Validate.notNull(args, "Arguments cannot be null");
    Validate.notNull(alias, "Alias cannot be null");

    if (args.length == 1) {
      return StringUtil.copyPartialMatches(args[0], DIFFICULTY_NAMES, new ArrayList(DIFFICULTY_NAMES.size()));
    }

    return ImmutableList.of();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy