Skip to content

Commit

Permalink
Ally command updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bramhaag committed Feb 1, 2017
1 parent 843ddef commit 1453c9e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 45 deletions.
49 changes: 49 additions & 0 deletions src/main/java/me/bramhaag/guilds/commands/CommandAlly.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
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.guild.GuildMember;
import me.bramhaag.guilds.guild.GuildRole;
import me.bramhaag.guilds.message.Message;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.Arrays;

public class CommandAlly extends CommandBase {

public CommandAlly() {
Expand All @@ -28,20 +33,64 @@ public void execute(Player player, String[] args) {

GuildRole role = GuildRole.getRole(guild.getMember(player.getUniqueId()).getRole());

if(args[0].equals("accept")) {
if(!role.canAddAlly()) {
return;
}

if(!guild.getPendingAllies().contains(targetGuild.getName())) {
Message.sendMessage(player, Message.COMMAND_ALLY_GUILD_NOT_PENDING);
return;
}

guild.addAlly(targetGuild);
targetGuild.addAlly(guild);

guild.getMembers().forEach(member -> Message.sendMessage(Bukkit.getPlayer(member.getUniqueId()), Message.COMMAND_ALLY_ACCEPTED.replace("guild", targetGuild.getName())));
guild.getMembers().forEach(member -> Message.sendMessage(Bukkit.getPlayer(member.getUniqueId()), Message.COMMAND_ALLY_ACCEPTED_TARGET.replace("guild", guild.getName())));
}
else if(args[0].equalsIgnoreCase("decline")) {
if(!role.canAddAlly()) {
return;
}

if(!guild.getPendingAllies().contains(targetGuild.getName())) {
Message.sendMessage(player, Message.COMMAND_ALLY_GUILD_NOT_PENDING);
return;
}

guild.getMembers().stream().filter(member -> GuildRole.getRole(member.getRole()).canAddAlly()).forEach(member -> Message.sendMessage(Bukkit.getPlayer(member.getUniqueId()), Message.COMMAND_ALLY_DECLINED.replace("guild", targetGuild.getName())));
guild.getMembers().stream().filter(member -> GuildRole.getRole(member.getRole()).canAddAlly()).forEach(member -> Message.sendMessage(Bukkit.getPlayer(member.getUniqueId()), Message.COMMAND_ALLY_DECLINED.replace("guild", guild.getName())));

}
if(args[0].equalsIgnoreCase("add")) {
if(!role.canAddAlly()) {
return;
}

targetGuild.addPendingAlly(guild);
Message.sendMessage(player, Message.COMMAND_ALLY_SEND);
}
else if(args[0].equalsIgnoreCase("remove")) {
if(!role.canRemoveAlly()) {
return;
}

guild.removeAlly(targetGuild);
targetGuild.removeAlly(guild);

guild.sendMessage(Message.COMMAND_ALLY_REMOVED.replace("guild", targetGuild.getName()));
targetGuild.sendMessage(Message.COMMAND_ALLY_REMOVED_TARGET.replace("guild", guild.getName()));
}
else if(args[0].equalsIgnoreCase("chat")) {
if(!role.useAllyChat()) {
return;
}

String message = String.format("[%s] [%s] %s", guild.getName(), role.getName(), String.join(" ", Arrays.copyOfRange(args, 2, args.length)));

guild.sendMessage(message);
targetGuild.sendMessage(message);
}
}
}
97 changes: 53 additions & 44 deletions src/main/java/me/bramhaag/guilds/guild/Guild.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ public class Guild {
@Expose
private List<GuildMember> members;

@Expose
private List<String> allies;

@Expose
private List<UUID> invitedMembers;

@Expose
private List<Guild> allies;
private List<String> pendingAllies;

public Guild(String name) {
this.name = name;
Expand Down Expand Up @@ -74,15 +77,7 @@ public void addMember(UUID uuid, GuildRole role) {
Main.getInstance().getScoreboardHandler().show(player);
}

Main.getInstance().getDatabaseProvider().updateGuild(this, (result, exception) -> {
if(!result) {
Main.getInstance().getLogger().log(Level.SEVERE, String.format("An error occurred while adding a member with an UUID of '%s' to guild '%s'", uuid, this.name));

if(exception != null) {
exception.printStackTrace();
}
}
});
updateGuild("An error occurred while adding a member with an UUID of '%s' to guild '%s'", uuid.toString(), this.name);
}

public void removeMember(UUID uuid) {
Expand Down Expand Up @@ -112,38 +107,19 @@ public void removeMember(UUID uuid) {

this.members.remove(member);

Main.getInstance().getDatabaseProvider().updateGuild(this, (result, exception) -> {
if(!result) {
Main.getInstance().getLogger().log(Level.SEVERE, String.format("An error occurred while removing a member with the UUID of '%s' from guild '%s'", uuid, this.name));
if(exception != null) {
exception.printStackTrace();
}
}
});
updateGuild("An error occurred while removing a member with the UUID of '%s' from guild '%s'", uuid.toString(), this.name);
}

public void inviteMember(UUID uuid) {
invitedMembers.add(uuid);
Main.getInstance().getDatabaseProvider().updateGuild(this, (result, exception) -> {
if(!result) {
Main.getInstance().getLogger().log(Level.SEVERE, String.format("An error occurred while inviting a member with the UUID of '%s' to guild '%s'", uuid, this.name));
if(exception != null) {
exception.printStackTrace();
}
}
});

updateGuild("An error occurred while inviting a member with the UUID of '%s' to guild '%s'", uuid.toString(), this.name);
}

public void removeInvitedPlayer(UUID uuid) {
invitedMembers.remove(uuid);
Main.getInstance().getDatabaseProvider().updateGuild(this, (result, exception) -> {
if(!result) {
Main.getInstance().getLogger().log(Level.SEVERE, String.format("An error occurred while removing an invited member member with the UUID of '%s' to guild '%s'", uuid, this.name));
if(exception != null) {
exception.printStackTrace();
}
}
});

updateGuild("An error occurred while removing an invited member member with the UUID of '%s' to guild '%s'", uuid.toString(), this.name);
}

public void sendMessage(String message) {
Expand All @@ -163,19 +139,12 @@ public void setPrefix(String prefix) {
public void updatePrefix(String prefix) {
setPrefix(prefix);

Main.getInstance().getDatabaseProvider().updateGuild(this, ((result, exception) -> {
if(!result) {
Main.getInstance().getLogger().log(Level.SEVERE, String.format("An error occurred while updating prefix to '%s' for guild '%s'", prefix, this.name));
if(exception != null) {
exception.printStackTrace();
}
}
}));
updateGuild("An error occurred while updating prefix to '%s' for guild '%s'", prefix, this.name);

Main.getInstance().getScoreboardHandler().update();
}

public List<Guild> getAllies() {
public List<String> getAllies() {
return allies;
}

Expand All @@ -195,7 +164,47 @@ public static boolean areAllies(UUID uuid1, UUID uuid2) {
Guild guild1 = getGuild(uuid1);
Guild guild2 = getGuild(uuid2);

return !(guild1 == null || guild2 == null) && guild1.getAllies().contains(guild2);
return !(guild1 == null || guild2 == null) && guild1.getAllies().contains(guild2.getName());

}

public void addAlly(Guild targetGuild) {
allies.add(targetGuild.getName());

updateGuild("Something went wrong while adding the ally %s from guild %s", targetGuild.getName(), this.getName());
}

public void removeAlly(Guild targetGuild) {
allies.remove(targetGuild.getName());

updateGuild("Something went wrong while removing the ally %s from guild %s", targetGuild.getName(), this.getName());
}

public void addPendingAlly(Guild targetGuild) {
pendingAllies.add(targetGuild.getName());

updateGuild("Something went wrong while adding pending ally %s from guild %s", targetGuild.getName(), this.getName());
}

public void removePendingAlly(Guild targetGuild) {
pendingAllies.remove(targetGuild.getName());

updateGuild("Something went wrong while removing pending ally %s from guild %s", targetGuild.getName(), this.getName());
}

public List<String> getPendingAllies() {
return pendingAllies;
}

private void updateGuild(String errorMessage, String... params) {
Main.getInstance().getDatabaseProvider().updateGuild(this, (result, exception) -> {
if(!result) {
Main.getInstance().getLogger().log(Level.SEVERE, String.format(errorMessage, params));

if(exception != null) {
exception.printStackTrace();
}
}
});
}
}
2 changes: 1 addition & 1 deletion src/main/java/me/bramhaag/guilds/message/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public enum Message {
COMMAND_UPDATE_FOUND,
COMMAND_UPDATE_NOT_FOUND,

EVENT_JOIN_PENDING_INVITES, COMMAND_ADMIN_PLAYER_ALREADY_IN_GUILD, COMMAND_ADMIN_ADDED_PLAYER, COMMAND_ADMIN_PLAYER_NOT_IN_GUILD, COMMAND_ADMIN_REMOVED_PLAYER;
EVENT_JOIN_PENDING_INVITES, COMMAND_ADMIN_PLAYER_ALREADY_IN_GUILD, COMMAND_ADMIN_ADDED_PLAYER, COMMAND_ADMIN_PLAYER_NOT_IN_GUILD, COMMAND_ADMIN_REMOVED_PLAYER, COMMAND_ALLY_GUILD_NOT_PENDING, COMMAND_ALLY_ACCEPTED, COMMAND_ALLY_ACCEPTED_TARGET, COMMAND_ALLY_DECLINED, COMMAND_ALLY_SEND, COMMAND_ALLY_REMOVED, COMMAND_ALLY_REMOVED_TARGET;

public static void sendMessage(CommandSender sender, Message message) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.PREFIX + Main.getInstance().getConfig().getString(getPath(message))));
Expand Down

0 comments on commit 1453c9e

Please sign in to comment.