mirror of
https://github.com/TotalFreedomMC/TF-Marriage.git
synced 2024-05-30 00:21:24 +00:00
Added marry command and some utils
This commit is contained in:
parent
10c3392136
commit
5ccbf9fbbb
|
@ -6,8 +6,18 @@ import org.bukkit.event.Listener;
|
|||
|
||||
import com.lenis0012.bukkit.marriage2.commands.Command;
|
||||
import com.lenis0012.bukkit.marriage2.internal.MarriagePlugin;
|
||||
import com.lenis0012.bukkit.marriage2.misc.BConfig;
|
||||
|
||||
public interface Marriage {
|
||||
|
||||
/**
|
||||
* Return a {@link com.lenis0012.bukkit.marriage2.misc.BConfig} from a YAML file.
|
||||
*
|
||||
* @param file File name.
|
||||
* @return Bukkit configuration instance.
|
||||
*/
|
||||
public BConfig getBukkitConfig(String file);
|
||||
|
||||
/**
|
||||
* Register a {@link org.bukkit.event.Listener} to this plugin.
|
||||
*
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
package com.lenis0012.bukkit.marriage2.commands;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.lenis0012.bukkit.marriage2.lang.Message;
|
||||
|
||||
public abstract class Command {
|
||||
private final String[] aliases;
|
||||
private String description = "No description available";
|
||||
|
@ -36,6 +39,21 @@ public abstract class Command {
|
|||
return getArgAsInt(index, 0);
|
||||
}
|
||||
|
||||
protected Player getArgAsPlayer(int index) {
|
||||
String name = getArg(0);
|
||||
for(Player player : Bukkit.getOnlinePlayers()) {
|
||||
if(player.getName().equalsIgnoreCase(name)) {
|
||||
return player;
|
||||
}
|
||||
} for(Player player : Bukkit.getOnlinePlayers()) {
|
||||
if(player.getName().toLowerCase().contains(name.toLowerCase())) {
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected int getArgLength() {
|
||||
return args.length - 1;
|
||||
}
|
||||
|
@ -48,6 +66,10 @@ public abstract class Command {
|
|||
}
|
||||
}
|
||||
|
||||
protected void reply(Message message, Object... args) {
|
||||
reply(message.toString(), args);
|
||||
}
|
||||
|
||||
protected void reply(String message, Object... args) {
|
||||
message = ChatColor.translateAlternateColorCodes('&', String.format(message, args));
|
||||
sender.sendMessage(message);
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package com.lenis0012.bukkit.marriage2.commands;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.lenis0012.bukkit.marriage2.lang.Message;
|
||||
|
||||
public class CommandMarry extends Command {
|
||||
|
||||
public CommandMarry(){
|
||||
super("marry");
|
||||
setDescription("Request a marriage with another player.");
|
||||
setUsage("<player>");
|
||||
|
||||
setMinArgs(0);
|
||||
setAllowConsole(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
Player target = getArgAsPlayer(-1);
|
||||
if(target != null) {
|
||||
if(System.out != null) { //TODO: Check if married
|
||||
//TODO: Handle command delay
|
||||
//TODO: Send marry request
|
||||
} else {
|
||||
reply(Message.ALREADY_MARRIED, getArg(-1));
|
||||
}
|
||||
} else {
|
||||
reply(Message.PLAYER_NOT_FOUND, getArg(-1));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package com.lenis0012.bukkit.marriage2.internal;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
@ -7,6 +8,7 @@ import org.bukkit.event.Listener;
|
|||
|
||||
import com.lenis0012.bukkit.marriage2.Marriage;
|
||||
import com.lenis0012.bukkit.marriage2.commands.Command;
|
||||
import com.lenis0012.bukkit.marriage2.misc.BConfig;
|
||||
|
||||
public abstract class MarriageBase implements Marriage {
|
||||
protected final MarriagePlugin plugin;
|
||||
|
@ -28,6 +30,12 @@ public abstract class MarriageBase implements Marriage {
|
|||
commandExecutor.regster(commandClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BConfig getBukkitConfig(String fileName) {
|
||||
File file = new File(fileName);
|
||||
return new BConfig(this, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Logger getLogger() {
|
||||
return plugin.getLogger();
|
||||
|
|
|
@ -41,7 +41,7 @@ public class MarriageCommandExecutor implements CommandExecutor {
|
|||
sender.sendMessage(ChatColor.RED + "You are not permitted to use this command.");
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(ChatColor.RED + "You have not specified enouhg arguments for this command.");
|
||||
sender.sendMessage(ChatColor.RED + "You have not specified enough arguments for this command.");
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.lenis0012.bukkit.marriage2.internal;
|
||||
|
||||
import com.lenis0012.bukkit.marriage2.commands.CommandTest;
|
||||
import com.lenis0012.bukkit.marriage2.lang.Message;
|
||||
|
||||
public class MarriageCore extends MarriageBase {
|
||||
|
||||
|
@ -12,4 +13,9 @@ public class MarriageCore extends MarriageBase {
|
|||
public void registerCommands() {
|
||||
register(CommandTest.class);
|
||||
}
|
||||
|
||||
@Register(name = "messages", type = Register.Type.ENABLE, priority = 1)
|
||||
public void loadMessages() {
|
||||
Message.reloadAll(this);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,12 +9,17 @@ import java.util.logging.Level;
|
|||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class MarriagePlugin extends JavaPlugin {
|
||||
private static MarriageCore core;
|
||||
|
||||
public static MarriageCore getCore() {
|
||||
return core;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private final List<Method>[] methods = new List[Register.Type.values().length];
|
||||
private final MarriageCore core;
|
||||
|
||||
public MarriagePlugin() {
|
||||
this.core = new MarriageCore(this);
|
||||
core = new MarriageCore(this);
|
||||
|
||||
//Scan methods
|
||||
Arrays.fill(methods, new ArrayList<Method>());
|
||||
|
|
|
@ -5,7 +5,7 @@ public @interface Register {
|
|||
|
||||
Type type();
|
||||
|
||||
int priority() default 0;
|
||||
int priority() default 5;
|
||||
|
||||
public static enum Type {
|
||||
LOAD("Completed plugin pre-load stage."),
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package com.lenis0012.bukkit.marriage2.lang;
|
||||
|
||||
import com.lenis0012.bukkit.marriage2.Marriage;
|
||||
import com.lenis0012.bukkit.marriage2.misc.BConfig;
|
||||
|
||||
public enum Message {
|
||||
PLAYER_NOT_FOUND("&cNo player named %s was found!"),
|
||||
ALREADY_MARRIED("&cPlayers %s is already married to someone!");
|
||||
|
||||
private final String defaultMessage;
|
||||
private String message;
|
||||
|
||||
private Message(String def) {
|
||||
this.defaultMessage = def;
|
||||
}
|
||||
|
||||
private void reload(BConfig config) {
|
||||
this.message = config.getOrSet(toString().toLowerCase(), defaultMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public static void reloadAll(Marriage marriage) {
|
||||
BConfig config = marriage.getBukkitConfig("messages.yml");
|
||||
for(Message message : Message.values()) {
|
||||
message.reload(config);
|
||||
}
|
||||
|
||||
config.save();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.lenis0012.bukkit.marriage2.misc;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import com.lenis0012.bukkit.marriage2.internal.MarriageBase;
|
||||
|
||||
/**
|
||||
* Represents a more easy version of FileConfiguration
|
||||
* Allows saving and reloading without throwing exceptions or needing the specify a file.
|
||||
*/
|
||||
public class BConfig extends YamlConfiguration {
|
||||
private final MarriageBase core;
|
||||
private final File file;
|
||||
|
||||
public BConfig(MarriageBase core, File file) {
|
||||
this.core = core;
|
||||
this.file = file;
|
||||
reload();
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
try {
|
||||
load(file);
|
||||
} catch (Exception e) {
|
||||
core.getLogger().log(Level.WARNING, "Failed to reload configuration file", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
try {
|
||||
save(file);
|
||||
} catch (Exception e) {
|
||||
core.getLogger().log(Level.WARNING, "Failed to save configuration file", e);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getOrSet(String key, T def) {
|
||||
if(contains(key)) {
|
||||
return (T) get(key);
|
||||
} else {
|
||||
set(key, def);
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T get(String key, Class<T> type) {
|
||||
return type.cast(get(key));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue