Skip to content

Commit

Permalink
Added #2
Browse files Browse the repository at this point in the history
  • Loading branch information
bramhaag committed Jan 5, 2017
1 parent 97c223d commit def5aa6
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 27 deletions.
3 changes: 3 additions & 0 deletions src/main/java/me/bramhaag/guilds/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public void onEnable() {
commandHandler.register(new CommandPrefix());
commandHandler.register(new CommandBoot());

commandHandler.register(new CommandAccept());
commandHandler.register(new CommandCancel());

commandHandler.register(new CommandReload());
commandHandler.register(new CommandHelp());

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/me/bramhaag/guilds/commands/CommandCancel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.bramhaag.guilds.commands;

import me.bramhaag.guilds.Main;
import me.bramhaag.guilds.commands.base.CommandBase;
import me.bramhaag.guilds.message.Message;
import me.bramhaag.guilds.util.ConfirmAction;
import org.bukkit.entity.Player;

public class CommandCancel extends CommandBase {

public CommandCancel() {
super("cancel", "Cancel an action", "guilds.command.cancel", false, new String[] { "decline" }, null, 0, 0);
}

public void execute(Player player, String[] args) {
ConfirmAction action = Main.getInstance().getCommandHandler().getActions().get(player);
if(action == null) {
Message.sendMessage(player, Message.COMMAND_CANCEL_ERROR);
}

action.decline();
}
}
25 changes: 25 additions & 0 deletions src/main/java/me/bramhaag/guilds/commands/CommandConfirm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.bramhaag.guilds.commands;

import me.bramhaag.guilds.Main;
import me.bramhaag.guilds.commands.base.CommandBase;
import me.bramhaag.guilds.message.Message;
import me.bramhaag.guilds.util.ConfirmAction;
import org.bukkit.entity.Player;

public class CommandConfirm extends CommandBase {

public CommandConfirm() {
super("confirm", "Confirm an action", "guilds.command.confirm", false, null, null, 0, 0);
}

public void execute(Player player, String[] args) {
ConfirmAction action = Main.getInstance().getCommandHandler().getActions().get(player);

if(action == null) {
Message.sendMessage(player, Message.COMMAND_CONFIRM_ERROR);
return;
}

action.accept();
}
}
40 changes: 28 additions & 12 deletions src/main/java/me/bramhaag/guilds/commands/CommandCreate.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.bramhaag.guilds.commands.base.CommandBase;
import me.bramhaag.guilds.guild.Guild;
import me.bramhaag.guilds.message.Message;
import me.bramhaag.guilds.util.ConfirmAction;
import org.bukkit.entity.Player;

import java.util.logging.Level;
Expand Down Expand Up @@ -38,21 +39,36 @@ public void execute(Player player, String[] args) {
}
}

Main.getInstance().getDatabaseProvider().createGuild(new Guild(args[0], player.getUniqueId()), ((result, exception) -> {
if(result) {
Message.sendMessage(player, Message.COMMAND_CREATE_SUCCESSFUL.replace("{guild}", args[0]));
Message.sendMessage(player, Message.COMMAND_CREATE_WARNING);

Main.getInstance().getScoreboardHandler().update();
Main.getInstance().getScoreboardHandler().show(player);
Main.getInstance().getCommandHandler().addAction(player, new ConfirmAction() {
@Override
public void accept() {
Main.getInstance().getDatabaseProvider().createGuild(new Guild(args[0], player.getUniqueId()), ((result, exception) -> {
if(result) {
Message.sendMessage(player, Message.COMMAND_CREATE_SUCCESSFUL.replace("{guild}", args[0]));

Main.getInstance().getScoreboardHandler().update();
Main.getInstance().getScoreboardHandler().show(player);
}
else {
Message.sendMessage(player, Message.COMMAND_CREATE_ERROR);

Main.getInstance().getLogger().log(Level.SEVERE, String.format("An error occurred while player '%s' was trying to create guild '%s'", player.getName(), args[0]));
if(exception != null) {
exception.printStackTrace();
}
}
}));

Main.getInstance().getCommandHandler().removeAction(player);
}
else {
Message.sendMessage(player, Message.COMMAND_CREATE_ERROR);

Main.getInstance().getLogger().log(Level.SEVERE, String.format("An error occurred while player '%s' was trying to create guild '%s'", player.getName(), args[0]));
if(exception != null) {
exception.printStackTrace();
}
@Override
public void decline() {
Message.sendMessage(player, Message.COMMAND_CREATE_CANCELLED);
Main.getInstance().getCommandHandler().removeAction(player);
}
}));
});
}
}
38 changes: 27 additions & 11 deletions src/main/java/me/bramhaag/guilds/commands/CommandDelete.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.bramhaag.guilds.commands.base.CommandBase;
import me.bramhaag.guilds.guild.Guild;
import me.bramhaag.guilds.message.Message;
import me.bramhaag.guilds.util.ConfirmAction;
import org.bukkit.entity.Player;

import java.util.logging.Level;
Expand All @@ -28,19 +29,34 @@ public void execute(Player player, String[] args) {
return;
}

Main.getInstance().getDatabaseProvider().removeGuild(guild, ((result, exception) -> {
if(result) {
Message.sendMessage(player, Message.COMMAND_DELETE_SUCCESSFUL.replace("{guild}", guild.getName()));
Main.getInstance().getScoreboardHandler().update();
Message.sendMessage(player, Message.COMMAND_DELETE_WARNING.replace("{guild}", guild.getName()));

Main.getInstance().getCommandHandler().addAction(player, new ConfirmAction() {
@Override
public void accept() {
Main.getInstance().getDatabaseProvider().removeGuild(guild, ((result, exception) -> {
if(result) {
Message.sendMessage(player, Message.COMMAND_DELETE_SUCCESSFUL.replace("{guild}", guild.getName()));
Main.getInstance().getScoreboardHandler().update();
}
else {
Message.sendMessage(player, Message.COMMAND_DELETE_ERROR);

Main.getInstance().getLogger().log(Level.SEVERE, String.format("An error occurred while player '%s' was trying to delete guild '%s'", player.getName(), guild.getName()));
if(exception != null) {
exception.printStackTrace();
}
}
}));

Main.getInstance().getCommandHandler().removeAction(player);
}
else {
Message.sendMessage(player, Message.COMMAND_DELETE_ERROR);

Main.getInstance().getLogger().log(Level.SEVERE, String.format("An error occurred while player '%s' was trying to delete guild '%s'", player.getName(), guild.getName()));
if(exception != null) {
exception.printStackTrace();
}
@Override
public void decline() {
Message.sendMessage(player, Message.COMMAND_DELETE_CANCELLED);
Main.getInstance().getCommandHandler().removeAction(player);
}
}));
});
}
}
27 changes: 25 additions & 2 deletions src/main/java/me/bramhaag/guilds/commands/CommandLeave.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package me.bramhaag.guilds.commands;

import me.bramhaag.guilds.Main;
import me.bramhaag.guilds.commands.base.CommandBase;
import me.bramhaag.guilds.guild.Guild;
import me.bramhaag.guilds.message.Message;
import me.bramhaag.guilds.util.ConfirmAction;
import org.bukkit.entity.Player;

public class CommandLeave extends CommandBase {
Expand All @@ -18,8 +20,29 @@ public void execute(Player player, String[] args) {
return;
}

guild.removeMember(player.getUniqueId());
Message.sendMessage(player, Message.COMMAND_LEAVE_SUCCESSFUL);
if(guild.getGuildMaster().getUniqueId().equals(player.getUniqueId())) {
Message.sendMessage(player, Message.COMMAND_LEAVE_WARNING_GUILDMASTER);
}
else {
Message.sendMessage(player, Message.COMMAND_LEAVE_WARNING);
}

Main.getInstance().getCommandHandler().addAction(player, new ConfirmAction() {
@Override
public void accept() {
guild.removeMember(player.getUniqueId());
Message.sendMessage(player, Message.COMMAND_LEAVE_SUCCESSFUL);

Main.getInstance().getCommandHandler().removeAction(player);
}

@Override
public void decline() {
Message.sendMessage(player, Message.COMMAND_LEAVE_CANCELLED);

Main.getInstance().getCommandHandler().removeAction(player);
}
});

guild.sendMessage(Message.COMMAND_LEAVE_PLAYER_LEFT.replace("{player}", player.getName()));
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/me/bramhaag/guilds/commands/base/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@

import me.bramhaag.guilds.IHandler;
import me.bramhaag.guilds.message.Message;
import me.bramhaag.guilds.util.ConfirmAction;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class CommandHandler implements CommandExecutor, IHandler {

private List<CommandBase> commands;
private HashMap<Player, ConfirmAction> actions;

@Override
public void enable() {
commands = new ArrayList<>();
actions = new HashMap<>();
}

@Override
public void disable() {
commands.clear();
commands = null;

actions.clear();
actions = null;
}

public void register(CommandBase command) {
Expand Down Expand Up @@ -85,4 +92,18 @@ private CommandBase getCommand(String name) {
public List<CommandBase> getCommands() {
return commands;
}

public HashMap<Player, ConfirmAction> getActions() {
return actions;
}

public ConfirmAction addAction(Player player, ConfirmAction action) {
actions.put(player, action);

return action;
}

public void removeAction(Player player) {
actions.remove(player);
}
}
11 changes: 11 additions & 0 deletions src/main/java/me/bramhaag/guilds/message/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ public enum Message {
COMMAND_ROLE_PLAYERS,

COMMAND_CREATE_SUCCESSFUL,
COMMAND_CREATE_CANCELLED,
COMMAND_CREATE_WARNING,
COMMAND_CREATE_ERROR,
COMMAND_CREATE_REQUIREMENTS,
COMMAND_CREATE_GUILD_NAME_TAKEN,

COMMAND_DELETE_SUCCESSFUL,
COMMAND_DELETE_CANCELLED,
COMMAND_DELETE_WARNING,
COMMAND_DELETE_ERROR,

COMMAND_INFO_HEADER,
Expand Down Expand Up @@ -60,6 +64,9 @@ public enum Message {
COMMAND_INVITE_SUCCESSFUL,

COMMAND_LEAVE_SUCCESSFUL,
COMMAND_LEAVE_CANCELLED,
COMMAND_LEAVE_WARNING,
COMMAND_LEAVE_WARNING_GUILDMASTER,
COMMAND_LEAVE_PLAYER_LEFT,

COMMAND_BOOT_SUCCESSFUL,
Expand All @@ -69,6 +76,10 @@ public enum Message {
COMMAND_PREFIX_REQUIREMENTS,
COMMAND_PREFIX_SUCCESSFUL,

COMMAND_CONFIRM_ERROR,

COMMAND_CANCEL_ERROR,

EVENT_JOIN_PENDING_INVITES;

public static void sendMessage(CommandSender sender, Message message) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/me/bramhaag/guilds/util/ConfirmAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.bramhaag.guilds.util;

public interface ConfirmAction {

void accept();
void decline();
}
11 changes: 11 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ messages:
players: "{player} - {role}"
create:
successful: "&aGuild '{guild}' created successfully!"
cancelled: "&cGuild creation cancelled!"
warning: "&cType /guilds confirm to create your guild, type /guilds cancel to cancel."
error: "&cSomething went wrong while creating your guild!"
requirements: "&cYour guild's name does not match the requirements! You can only use alphanumeric characters and the length of the name cannot exceed 64"
guild-name-taken: "&cThis name is already taken!"
delete:
successful: "&aDeleted '{guild}' successfully!"
cancelled: "&cGuild deletion cancelled!"
warning: "&cType /guilds confirm to delete your guild, type /guilds cancel to cancel."
error: "&cSomething went wrong while deleting your guild!"
info:
header: "Information for {guild}"
Expand Down Expand Up @@ -107,6 +111,9 @@ messages:
successful: "&aYou've successfully invited {player} to your guild!"
leave:
successful: "&aYou've successfully left your guild!"
cancelled: "&cLeaving guild cancelled!"
warning: "&cType /guilds confirm to leave your guild, type /guilds cancel to cancel."
warning-guildmaster: "&cYou're the Guild Master of this guild, leaving the guild will mean that the guild is deleted. Type /guilds confirm to leave and delete your guild, type /guilds cancel to cancel."
player-left: "&cPlayer '{player}' left your guild!"
boot:
successful: "&aSuccessfully kicked {player} from your guild!"
Expand All @@ -115,6 +122,10 @@ messages:
prefix:
successful: "&aGuild's prefix changed successfully!"
requirements: "&cYour guild's prefix does not match the requirements! You can only use alphanumeric characters and the length of the prefix cannot exceed 8"
confirm:
error: "&cYou have no actions to confirm!"
cancel:
error: "&cYou have no actions to cancel!"
event:
join:
pending-invites: "&aYou have {number} pending invite(s) from the guild(s): &e{guilds}"
6 changes: 4 additions & 2 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ commands:
description: See the Guild commands

permissions:
guilds.*:
description: All permissions for the Guilds plugin
guilds.command.*:
description: All commands for the Guilds plugin
children:
guilds.command.accept: true
guilds.command.boot: true
Expand All @@ -26,4 +26,6 @@ permissions:
guilds.command.prefix: true
guilds.command.promote: true
guilds.command.role: true
guilds.command.reload: true
guilds.command.confirm: true
default: true

0 comments on commit def5aa6

Please sign in to comment.