下面列出了怎么用org.bukkit.Difficulty的API类实例代码及写法,或者点击链接到github查看源代码。
public MapInfoImpl(Element root) throws InvalidXMLException {
this(
checkNotNull(root).getChildTextNormalize("slug"),
XMLUtils.parseSemanticVersion(Node.fromRequiredAttr(root, "proto")),
XMLUtils.parseSemanticVersion(Node.fromRequiredChildOrAttr(root, "version")),
Node.fromRequiredChildOrAttr(root, "name").getValueNormalize(),
Node.fromRequiredChildOrAttr(root, "objective", "description").getValueNormalize(),
parseContributors(root, "author"),
parseContributors(root, "contributor"),
parseRules(root),
XMLUtils.parseEnum(
Node.fromLastChildOrAttr(root, "difficulty"),
Difficulty.class,
"difficulty",
Difficulty.NORMAL)
.ordinal(),
null,
null,
parseWorld(root),
XMLUtils.parseFormattedText(root, "game"));
}
@Override
public ModuleCollection<MapDifficulty> load(Match match) {
ModuleCollection<MapDifficulty> results = new ModuleCollection<>();
try {
switch (Numbers.parseInt(match.getDocument().getRootElement().getChildText("difficulty"))) {
case 0:
results.add(new MapDifficulty(Difficulty.PEACEFUL));
break;
case 1:
results.add(new MapDifficulty(Difficulty.EASY));
break;
case 2:
results.add(new MapDifficulty(Difficulty.NORMAL));
break;
case 3:
results.add(new MapDifficulty(Difficulty.HARD));
break;
}
} catch (NumberFormatException | NullPointerException e) {
}
return results;
}
public MapInfoImpl(
@Nullable String id,
Version proto,
Version version,
String name,
String description,
@Nullable Collection<Contributor> authors,
@Nullable Collection<Contributor> contributors,
@Nullable Collection<String> rules,
@Nullable Integer difficulty,
@Nullable Collection<MapTag> tags,
@Nullable Collection<Integer> players,
@Nullable WorldInfo world,
Component gamemode) {
this.name = checkNotNull(name);
this.id = checkNotNull(MapInfo.normalizeName(id == null ? name : id));
this.proto = checkNotNull(proto);
this.version = checkNotNull(version);
this.description = checkNotNull(description);
this.authors = authors == null ? new LinkedList<>() : authors;
this.contributors = contributors == null ? new LinkedList<>() : contributors;
this.rules = rules == null ? new LinkedList<>() : rules;
this.difficulty = difficulty == null ? Difficulty.NORMAL.ordinal() : difficulty;
this.tags = tags == null ? new TreeSet<>() : tags;
this.players = players == null ? new LinkedList<>() : players;
this.world = world == null ? new WorldInfoImpl() : world;
this.gamemode = gamemode == null ? TextComponent.empty() : gamemode;
}
public MapInfo(SemanticVersion proto,
@Nullable String slug,
String name,
SemanticVersion version,
MapDoc.Edition edition,
MapDoc.Phase phase,
@Nullable BaseComponent game,
MapDoc.Genre genre,
Set<MapDoc.Gamemode> gamemodes,
BaseComponent objective,
List<Contributor> authors,
List<Contributor> contributors,
List<String> rules,
@Nullable Difficulty difficulty,
Environment dimension,
boolean friendlyFire) {
this.id = new MapId(slug != null ? slug : MapId.slugifyName(name), edition, phase);
this.proto = checkNotNull(proto);
this.name = checkNotNull(name);
this.version = checkNotNull(version);
this.game = game;
this.genre = checkNotNull(genre);
this.gamemodes = checkNotNull(gamemodes);
this.objective = checkNotNull(objective);
this.authors = checkNotNull(authors);
this.contributors = checkNotNull(contributors);
this.rules = checkNotNull(rules);
this.difficulty = difficulty;
this.dimension = checkNotNull(dimension);
this.friendlyFire = friendlyFire;
}
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.SET)
return CollectionUtils.array(Difficulty.class);
return null;
}
@Override
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
if (delta == null)
return;
Difficulty difficulty = (Difficulty) delta[0];
for (World world : getExpr().getArray(e)) {
world.setDifficulty(difficulty);
}
}
public NickNamerSelfUpdateEvent(Player player, String name, Object gameProfile,Difficulty difficulty, GameMode gameMode, boolean async) {
super(async);
this.player = player;
this.name = name;
this.gameProfile = gameProfile;
this.difficulty = difficulty;
this.gameMode = gameMode;
}
public NickNamerSelfUpdateEvent(Player player, String name, Object gameProfile,Difficulty difficulty, GameMode gameMode) {
this.player = player;
this.name = name;
this.gameProfile = gameProfile;
this.difficulty = difficulty;
this.gameMode = gameMode;
}
@Override
public void onEnable() {
for (final World world : Bukkit.getWorlds()) {
if (worlds.worldMatches(world)) {
world.setDifficulty(Difficulty.HARD);
}
}
}
@EventHandler
public void on(WorldLoadEvent event) {
if (isEnabled()) {
if (worlds.worldMatches(event.getWorld())) {
event.getWorld().setDifficulty(Difficulty.HARD);
}
}
}
@Override
public InfoModule parse(MapModuleContext context, Logger logger, Document doc) throws InvalidXMLException {
Element root = doc.getRootElement();
String name = Node.fromRequiredChildOrAttr(root, "name").getValueNormalize();
SemanticVersion version = XMLUtils.parseSemanticVersion(Node.fromRequiredChildOrAttr(root, "version"));
MapDoc.Phase phase = XMLUtils.parseEnum(Node.fromLastChildOrAttr(root, "phase"), MapDoc.Phase.class, "phase", MapDoc.Phase.PRODUCTION);
MapDoc.Edition edition = XMLUtils.parseEnum(Node.fromLastChildOrAttr(root, "edition"), MapDoc.Edition.class, "edition", MapDoc.Edition.STANDARD);
// Allow multiple <objective> elements, so include files can provide defaults
final BaseComponent objective = XMLUtils.parseLocalizedText(Node.fromRequiredLastChildOrAttr(root, "objective"));
String slug = root.getChildTextNormalize("slug");
BaseComponent game = XMLUtils.parseFormattedText(root, "game");
MapDoc.Genre genre = XMLUtils.parseEnum(Node.fromNullable(root.getChild("genre")), MapDoc.Genre.class, "genre", MapDoc.Genre.OTHER);
final TreeSet<MapDoc.Gamemode> gamemodes = new TreeSet<>();
for(Element elGamemode : root.getChildren("gamemode")) {
gamemodes.add(XMLUtils.parseEnum(elGamemode, MapDoc.Gamemode.class));
}
List<Contributor> authors = readContributorList(root, "authors", "author");
if(game == null) {
Element blitz = root.getChild("blitz");
if(blitz != null) {
Element title = blitz.getChild("title");
if(title != null) {
if(context.getProto().isNoOlderThan(ProtoVersions.REMOVE_BLITZ_TITLE)) {
throw new InvalidXMLException("<title> inside <blitz> is no longer supported, use <map game=\"...\">", title);
}
game = new Component(title.getTextNormalize());
}
}
}
List<Contributor> contributors = readContributorList(root, "contributors", "contributor");
List<String> rules = new ArrayList<String>();
for(Element parent : root.getChildren("rules")) {
for(Element rule : parent.getChildren("rule")) {
rules.add(rule.getTextNormalize());
}
}
Difficulty difficulty = XMLUtils.parseEnum(Node.fromLastChildOrAttr(root, "difficulty"), Difficulty.class, "difficulty");
Environment dimension = XMLUtils.parseEnum(Node.fromLastChildOrAttr(root, "dimension"), Environment.class, "dimension", Environment.NORMAL);
boolean friendlyFire = XMLUtils.parseBoolean(Node.fromLastChildOrAttr(root, "friendly-fire", "friendlyfire"), false);
return new InfoModule(new MapInfo(context.getProto(), slug, name, version, edition, phase, game, genre, ImmutableSet.copyOf(gamemodes), objective, authors, contributors, rules, difficulty, dimension, friendlyFire));
}
public Difficulty getGameDifficulty() {
return gameDifficulty;
}
@Override
@Nullable
public Difficulty convert(World world) {
return world.getDifficulty();
}
@Override
public Class<Difficulty> getReturnType() {
return Difficulty.class;
}
public Difficulty getDifficulty() {
return difficulty;
}
public void setDifficulty(Difficulty difficulty) {
this.difficulty = difficulty;
}
public void setDifficulty(Difficulty difficulty) {
this.getHandle().difficultySetting = net.minecraft.world.EnumDifficulty.getDifficultyEnum(difficulty.getValue());
}
public Difficulty getDifficulty() {
return Difficulty.getByValue(this.getHandle().difficultySetting.ordinal());
}
@Override
public void setDifficulty(Difficulty difficulty) {
parent.setDifficulty(difficulty);
}
@Override
public Difficulty getDifficulty() {
return parent.getDifficulty();
}
protected MapDifficulty(Difficulty difficulty) {
GameHandler.getGameHandler().getMatchWorld().setDifficulty(difficulty);
}