Files
src
pom.xml
README.md
Code
15 1,458
src/main/java/com/gamemodeselector/GamemodeSelector.java
package com.gamemodeselector;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
public final class GamemodeSelector extends JavaPlugin {
public static final String BUNGEE_CHANNEL = "BungeeCord";
private final Cooldown openCooldown = new Cooldown();
private final Cooldown clickCooldown = new Cooldown();
private CompassManager compass;
private ActionExecutor actions;
private List<ModeDefinition> modes = List.of();
private Set<String> enabledWorlds = Set.of();
private boolean allWorlds;
@Override
public void onEnable() {
saveDefaultConfig();
loadSettings();
this.compass = new CompassManager(this);
this.actions = new ActionExecutor(this);
getServer().getPluginManager().registerEvents(new CompassListener(this), this);
getServer().getPluginManager().registerEvents(new GuiListener(this), this);
getServer().getMessenger().registerOutgoingPluginChannel(this, BUNGEE_CHANNEL);
PluginCommand command = getCommand("gamemodeselector");
if (command != null) {
SelectorCommand handler = new SelectorCommand(this);
command.setExecutor(handler);
command.setTabCompleter(handler);
}
// Covers a /reload or a plugin that is enabled while players are already online.
for (Player player : getServer().getOnlinePlayers()) {
compass.update(player, true);
}
getLogger().info("Loaded " + modes.size() + " gamemode(s); enabled in "
+ (allWorlds ? "all worlds" : enabledWorlds.size() + " world(s)") + ".");
}
@Override
public void onDisable() {
getServer().getMessenger().unregisterOutgoingPluginChannel(this);
}
/** Re-reads config.yml and refreshes the compass of everyone online. */
public void reloadAll() {
reloadConfig();
loadSettings();
for (Player player : getServer().getOnlinePlayers()) {
compass.update(player, true);
}
}
private void loadSettings() {
// enabled worlds
Set<String> worlds = new HashSet<>();
boolean all = false;
for (String name : getConfig().getStringList("enabled-worlds")) {
String trimmed = name.trim();
if (trimmed.equals("*")) {
all = true;
} else if (!trimmed.isEmpty()) {
worlds.add(trimmed.toLowerCase(Locale.ROOT));
}
}
this.allWorlds = all;
this.enabledWorlds = Collections.unmodifiableSet(worlds);
if (!all && worlds.isEmpty()) {
getLogger().warning("enabled-worlds is empty - the compass will not be given anywhere.");
}
// gamemode blocks
List<ModeDefinition> loaded = new ArrayList<>();
ConfigurationSection section = getConfig().getConfigurationSection("modes");
if (section != null) {
for (String id : section.getKeys(false)) {
ConfigurationSection mode = section.getConfigurationSection(id);
if (mode == null || !mode.getBoolean("enabled", true)) {
continue;
}
loaded.add(ModeDefinition.parse(id, mode));
}
}
if (loaded.isEmpty()) {
getLogger().warning("No gamemodes are defined (or enabled) under 'modes' in config.yml.");
}
this.modes = Collections.unmodifiableList(loaded);
}
public boolean isEnabledWorld(World world) {
return allWorlds || enabledWorlds.contains(world.getName().toLowerCase(Locale.ROOT));
}
public List<ModeDefinition> getModes() {
return modes;
}
public CompassManager compass() {
return compass;
}
public ActionExecutor actions() {
return actions;
}
public Cooldown openCooldown() {
return openCooldown;
}
public Cooldown clickCooldown() {
return clickCooldown;
}
/**
* Sends a message from the "messages" section. Extra arguments are placeholder pairs,
* e.g. send(sender, "given-to", "%player%", "Steve").
*/
public void send(CommandSender to, String key, String... replacements) {
String message = getConfig().getString("messages." + key, "");
if (message == null || message.isEmpty()) {
return;
}
for (int i = 0; i + 1 < replacements.length; i += 2) {
message = message.replace(replacements[i], replacements[i + 1]);
}
String prefix = getConfig().getString("messages.prefix", "");
to.sendMessage(Text.parse(prefix + message));
}
}