Skip to content

Commit

Permalink
Suporte inicial SimpleClans - Parte III
Browse files Browse the repository at this point in the history
  • Loading branch information
SrBedrock committed Oct 9, 2023
1 parent 4e5f28b commit 92d3fef
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: Gradle build
on:
workflow_dispatch:
push:
branches: [ main ]
branches: [ "main", "dev/*" ]
pull_request:
branches: [ main ]
branches: [ "main", "dev/*" ]

jobs:
build:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class IgnoreCommand extends BaseCommand {

private final ChatChatPlugin plugin;
private final static String IGNORE_PERMISSION = "chatchat.ignore";
private static final String IGNORE_PERMISSION = "chatchat.ignore";
public static final String IGNORE_BYPASS_PERMISSION = IGNORE_PERMISSION + ".bypass";

public IgnoreCommand(final ChatChatPlugin plugin) {
Expand All @@ -36,4 +36,5 @@ public void ignore(ChatUser sender, ChatUser target) {
sender.sendMessage(plugin.configManager().messages().ignoredPlayer()
.replaceText(builder -> builder.matchLiteral("<player>").replacement(target.player().getDisplayName())));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Command("ignorelist")
public class IgnoreListCommand extends BaseCommand {
private final ChatChatPlugin plugin;
private final static String IGNORELIST_PERMISSION = "chatchat.ignorelist";
private static final String IGNORELIST_PERMISSION = "chatchat.ignorelist";

public IgnoreListCommand(final ChatChatPlugin plugin) {
this.plugin = plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import at.helpch.chatchat.ChatChatPlugin;
import at.helpch.chatchat.api.user.ChatUser;
import at.helpch.chatchat.hooks.simpleclans.AbstractSimpleClansChannel;
import at.helpch.chatchat.hooks.towny.AbstractTownyChannel;
import at.helpch.chatchat.util.MessageProcessor;
import com.palmergames.bukkit.towny.TownyUniverse;
Expand Down Expand Up @@ -45,6 +46,14 @@ public void switchChannel(final ChatUser user, @Join @Optional @NotNull final St
}
}

if (channel instanceof AbstractSimpleClansChannel simpleClansChannel) {
final var clanPlayer = simpleClansChannel.getClanManager().getClanPlayer(user.uuid());
if (clanPlayer == null || clanPlayer.getClan() == null) {
user.sendMessage(plugin.configManager().messages().userNotInClan());
return;
}
}

if (!channel.isUsableBy(user)) {
user.sendMessage(plugin.configManager().messages().channelNoPermission());
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class UnignoreCommand extends BaseCommand {

private final ChatChatPlugin plugin;
private final static String IGNORE_PERMISSION = "chatchat.ignore";
private static final String IGNORE_PERMISSION = "chatchat.ignore";

public UnignoreCommand(final ChatChatPlugin plugin) {
this.plugin = plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public final class MessagesHolder {
private Component playersOnly = text("Only players can do this!", RED);
private Component userOffline = text("The user is not online!", RED);
private Component userNotInTown = text("You are not in a town!", RED);
private Component userNotInClan = text("You are not in a clan!", RED);

// messaging related
private Component noReplies = text("You have no one to reply to!", RED);
Expand Down Expand Up @@ -89,6 +90,10 @@ public final class MessagesHolder {
return userNotInTown;
}

public @NotNull Component userNotInClan() {
return userNotInClan;
}

public @NotNull Component noReplies() {
return noReplies;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import at.helpch.chatchat.util.ChannelUtils;
import net.sacredlabyrinth.phaed.simpleclans.ClanPlayer;
import net.sacredlabyrinth.phaed.simpleclans.SimpleClans;
import net.sacredlabyrinth.phaed.simpleclans.managers.ClanManager;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -35,12 +37,15 @@ protected AbstractSimpleClansChannel(@NotNull final String name,
}

private @NotNull Optional<ClanPlayer> clanPlayerList(@NotNull final UUID uuid) {
var clanManager = SimpleClans.getInstance().getClanManager();
return Objects.requireNonNull(clanManager.getClanByPlayerUniqueId(uuid)).getOnlineMembers().stream()
return Objects.requireNonNull(getClanManager().getClanByPlayerUniqueId(uuid)).getOnlineMembers().stream()
.filter(clanPlayer -> clanPlayer.getUniqueId().equals(uuid))
.findFirst();
}

public ClanManager getClanManager() {
return SimpleClans.getInstance().getClanManager();
}

@Override
public boolean isUsableBy(@NotNull final ChatUser user) {
return super.isUsableBy(user) && clanPlayerList(user.uuid()).isPresent();
Expand All @@ -63,4 +68,21 @@ public Set<User> targets(final @NotNull User source) {
.collect(Collectors.toSet());
}

@Contract(value = "null -> false", pure = true)
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;

AbstractSimpleClansChannel that = (AbstractSimpleClansChannel) o;

return plugin.equals(that.plugin);
}

@Override
public int hashCode() {
return plugin.hashCode();
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package at.helpch.chatchat.hooks.simpleclans;

import at.helpch.chatchat.api.holder.FormatsHolder;
import net.sacredlabyrinth.phaed.simpleclans.Clan;
import net.sacredlabyrinth.phaed.simpleclans.ClanPlayer;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

public final class SimpleClansAllyChannel extends AbstractSimpleClansChannel {

Expand All @@ -22,9 +23,11 @@ public SimpleClansAllyChannel(@NotNull final String name,

@Override
protected @NotNull Set<ClanPlayer> clanPlayerList(@NotNull final ClanPlayer clanPlayer) {
Set<ClanPlayer> clanPlayerSet = new HashSet<>(Objects.requireNonNull(clanPlayer.getClan()).getAllAllyMembers());
clanPlayerSet.add(clanPlayer);
return clanPlayerSet;
final Clan clan = clanPlayer.getClan();
if (clan == null) return Set.of();
Set<ClanPlayer> clanPlayerSet = new HashSet<>(clan.getAllAllyMembers());
clanPlayerSet.addAll(clan.getOnlineMembers());
return clanPlayerSet.stream().filter(cp -> cp.toPlayer() != null).collect(Collectors.toSet());
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package at.helpch.chatchat.hooks.simpleclans;

import at.helpch.chatchat.api.holder.FormatsHolder;
import net.sacredlabyrinth.phaed.simpleclans.Clan;
import net.sacredlabyrinth.phaed.simpleclans.ClanPlayer;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

public final class SimpleClansClanChannel extends AbstractSimpleClansChannel {
Expand All @@ -24,7 +24,9 @@ public SimpleClansClanChannel(@NotNull final String name,
@Contract("_ -> new")
@Override
protected @NotNull Set<ClanPlayer> clanPlayerList(@NotNull final ClanPlayer clanPlayer) {
return new HashSet<>(Objects.requireNonNull(clanPlayer.getClan()).getOnlineMembers());
final Clan clan = clanPlayer.getClan();
if (clan == null) return Set.of();
return new HashSet<>(clan.getOnlineMembers());
}

}
21 changes: 12 additions & 9 deletions plugin/src/main/java/at/helpch/chatchat/user/ChatUserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,10 @@ public boolean chatEnabled() {

@Override
public boolean canSee(@NotNull final User target) {
if (!(target instanceof ChatUser)) {
if (!(target instanceof ChatUser chatUser)) {
return true;
}

final var chatUser = (ChatUser) target;

final var plugin = JavaPlugin.getPlugin(ChatChatPlugin.class);

return plugin.hookManager()
Expand All @@ -169,7 +167,11 @@ public boolean canSee(@NotNull final User target) {

@Override
public @NotNull Player player() {
return Objects.requireNonNull(Bukkit.getPlayer(uuid)); // this will never be null
Player player = Bukkit.getPlayer(uuid);
if (player == null) {
throw new IllegalStateException("Player is not online!");
}
return player; // this will never be null
}

@Override
Expand All @@ -185,10 +187,11 @@ public boolean canSee(@NotNull final User target) {
@Override
public String toString() {
return "ChatUserImpl{" +
"uuid=" + uuid +
", lastMessaged=" + lastMessagedUser().map(ChatUser::uuid) +
", channel=" + channel +
", format=" + format +
'}';
"uuid=" + uuid +
", lastMessaged=" + lastMessagedUser().map(ChatUser::uuid) +
", channel=" + channel +
", format=" + format +
'}';
}

}

0 comments on commit 92d3fef

Please sign in to comment.