Skip to content

Instantly share code, notes, and snippets.

@aikar
Created February 27, 2014 05:57
Embed
What would you like to do?
package com.empireminecraft.commands.misc;
import com.empireminecraft.commands.EmpireCommand;
import com.empireminecraft.commands.EmpireCommandAlias;
import com.empireminecraft.commands.EmpireCommandCompletion;
import com.empireminecraft.commands.EmpireSubcommand;
import com.empireminecraft.config.EmpireServer;
import com.empireminecraft.config.PlayerSettingKey;
import com.empireminecraft.data.EmpireUser;
import com.empireminecraft.util.ChatAPI;
import com.empireminecraft.util.Util;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@EmpireCommandAlias("ps")
public class PlayerSettingCommand extends EmpireCommand {
public PlayerSettingCommand() {
super("psetting");
}
@EmpireSubcommand("set|gset")
@EmpireCommandCompletion("@psetting")
public void onSet(Player player, String[] args) {
boolean gset = origArgs[0].equals("gset");
final EmpireUser user = EmpireUser.getUser(player);
if (args.length < 1) {
promptSet(player);
return;
}
final PlayerSettingKey key = PlayerSettingKey.getByKey(args[0]);
if (key == null) {
Util.sendMsg(player, "&cInvalid key");
promptSet(player);
return;
}
if (args.length < 2) {
promptOptions(player, key);
return;
}
String val = args[1].toLowerCase();
if (!key.options.contains(val)) {
promptOptions(player, key);
return;
}
if (gset) {
for (EmpireServer server : EmpireServer.values()) {
user.setSetting(key, val, server);
}
ChatAPI.newMessage().msgColor("&bConfig ")
.msgColor("&a[" + key.label + "]").tooltip("&a" + key.desc)
.msgColor("&bset to &a" + val + "&b on all servers.")
.send(player);
} else {
user.setSetting(key, val);
ChatAPI.newMessage().msgColor("&bConfig ")
.msgColor("&a[" + key.label + "]").tooltip("&a" + key.desc)
.msgColor("&bset to &a" + val + "&b.")
.send(player);
}
}
private void promptOptions(Player player, PlayerSettingKey key) {
ChatAPI chat = ChatAPI.newMessage();
boolean gset = origArgs.length > 0 && origArgs[0].equals("gset");
chat.msgColor("&bSetting [&a" + key.label + "&b]" + (gset ? " (All Servers)" : "")).send(player);
chat.msgColor("&b " + key.desc).send(player);
chat.msg(" ");
int count = key.options.size();
for (String option : key.options) {
chat.msgColor("&a" + option)
.command("/ps " + (gset ? "gset" : "set") + " " + key.key + " " + option);
if (--count > 0) {
chat.msgColor("&f, ");
}
}
chat.send(player);
chat.msgColor("&bClick option to set it.").send(player);
}
private void promptSet(Player player) {
boolean gset = origArgs.length > 0 && origArgs[0].equals("gset");
ChatAPI chat = ChatAPI.newMessage();
chat.msgColor("&bPlease click a setting to change " + (gset ? "(All Servers)" : "") + ":");
chat.send(player);
chat.msg(" ");
final PlayerSettingKey[] values = PlayerSettingKey.values();
int count = values.length;
for (PlayerSettingKey key : values) {
chat.msgColor("&a[" + key.label + "]").tooltip("&a" + key.desc)
.command("/ps " + (gset ? "gset" : "set") + " " + key.key);
if (--count > 0) {
chat.msgColor("&f, ");
}
}
chat.send(player);
}
@Override
public void onDefault(CommandSender sender, String commandLabel) {
Player player = (Player) sender;
EmpireUser user = EmpireUser.getUser(player);
ChatAPI chat = ChatAPI.newMessage();
chat.msgColor("&bYour personal settings for this server:");
chat.send(player);
final PlayerSettingKey[] values = PlayerSettingKey.values();
int count = values.length;
for (PlayerSettingKey key : values) {
chat.msgColor("&e [" + key.label + "]").tooltip("&a" + key.desc)
.command("/ps set " + key.key);
chat.msgColor("&e: &a" + user.getSetting(key));
if (--count > 0) {
chat.msgColor("&e, ");
}
}
chat.send(player);
chat.msgColor("&bClick to Change Settings: ");
chat.msgColor("&b[&a" + EmpireServer.getServer().label + "&b]").command("/psetting set");
chat.msg(" ");
chat.msgColor("&b[&aAll&b]").command("/psetting gset");
chat.send(player);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment