Skip to content

Commit

Permalink
feat(party): party broadcast :O
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakShearman committed Mar 5, 2024
1 parent bd7f995 commit 99fcccd
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ WORKDIR /app/plugins
RUN wget -O viaversion.jar $VIA_VERSION_JAR_URL
COPY run/plugins/viaversion/config.yml viaversion/config.yml

RUN wget -O viabackwards.jar https://github.com/ViaVersion/ViaBackwards/releases/download/4.9.1/ViaBackwards-4.9.1.jar
RUN #wget -O viabackwards.jar https://github.com/ViaVersion/ViaBackwards/releases/download/4.9.1/ViaBackwards-4.9.1.jar
COPY build/libs/*-all.jar velocity-core.jar
# Go back to the base directory for our server
WORKDIR /app
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies {
annotationProcessor("com.velocitypowered:velocity-api:3.2.0-SNAPSHOT")
implementation("net.kyori:adventure-text-minimessage:4.14.0")

implementation("dev.emortal.api:common-proto-sdk:a893850")
implementation("dev.emortal.api:common-proto-sdk:cfb6fa4")
implementation("dev.emortal.api:agones-sdk:1.0.7")
implementation("dev.emortal.api:live-config-parser:8f566b9")
implementation("dev.emortal.api:module-system:1.0.0")
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/dev/emortal/velocity/lang/ChatMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public interface ChatMessages {
Args1<String> ERROR_PLAYER_INVITED_TO_PARTY = username -> red(username + " has already been invited to your party");
Args1<String> ERROR_PLAYER_IN_THIS_PARTY = username -> red(username + " is already in the party");
Args0 ERROR_YOU_NOT_INVITED_TO_PARTY = () -> red("You are not invited to this party");
Args0 ERROR_ALREADY_IN_PARTY = () -> red("You are already in a party. Use <click:run_command:/party leave>/party leave</click> to leave");
Args0 ERROR_ALREADY_IN_PARTY = () -> miniMessage("<red>You are already in a party. Use <click:run_command:/party leave>/party leave</click> to leave");
Args0 ERROR_CANNOT_KICK_LEADER = () -> red("You cannot kick the party leader");
Args1<String> ERROR_PLAYER_NOT_IN_PARTY = username -> red(username + " is not in your party");
Args0 ERROR_CANNOT_LEAVE_AS_LEADER = () -> Component.text()
Expand All @@ -121,6 +121,10 @@ public interface ChatMessages {
.build();
Args0 ERROR_YOU_NOT_IN_PARTY = () -> red("You are not in a party");

// /party broadcast
Args0 PARTY_BROADCAST_PARTY_CLOSED = () -> miniMessage("<red>Your party must be open to broadcast a message. Use <click:run_command:/party open><u>/party open</u></click> to open your party");
Args1<String> PARTY_BROADCAST_MESSAGE = leaderUsername -> miniMessage("<green>" + leaderUsername + " is hosting a party. <click:run_command:/party join " + leaderUsername + ">Click <u>here</u> to join!</click>");

Args1<String> ROLE_CREATED = role -> green("Role " + role + " created");
Args4<String, Integer, Integer, Component> ROLE_DESCRIPTION = (id, priority, permissions, displayName) ->
Component.text().color(NamedTextColor.LIGHT_PURPLE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public boolean onLoad() {
return true;
}

public @NotNull FriendlyKafkaProducer getKafkaProducer() {
if (this.kafkaProducer == null) throw new IllegalStateException("Kafka producer not available");
return this.kafkaProducer;
}

@Override
public void onUnload() {
if (this.kafkaProducer != null) this.kafkaProducer.shutdown();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/emortal/velocity/party/PartyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public boolean onLoad() {
PartyCache cache = new PartyCache(service);
new PartyUpdateListener(cache, super.adapters().playerProvider(), new ChatPartyUpdateNotifier(super.adapters().playerProvider()), messaging);

super.registerCommand(new PartyCommand(service, super.playerResolver(), super.adapters().commandManager().usernameSuggesters()));
super.registerCommand(new PartyCommand(service, super.playerResolver(), super.adapters().commandManager().usernameSuggesters(), messaging.getKafkaProducer()));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.emortal.velocity.party;

import com.velocitypowered.api.proxy.Player;
import dev.emortal.api.message.party.PartyBroadcastMessage;
import dev.emortal.api.message.party.PartyCreatedMessage;
import dev.emortal.api.message.party.PartyDeletedMessage;
import dev.emortal.api.message.party.PartyEmptiedMessage;
Expand Down Expand Up @@ -42,6 +43,7 @@ final class PartyUpdateListener {
messaging.addListener(PartyPlayerLeftMessage.class, this::handleLeaveParty);
messaging.addListener(PartyLeaderChangedMessage.class, this::handleLeaderChange);
messaging.addListener(PartyInviteCreatedMessage.class, this::handleInviteCreated);
messaging.addListener(PartyBroadcastMessage.class, this::handlePartyBroadcast);
}

void handleCreateParty(@NotNull PartyCreatedMessage message) {
Expand Down Expand Up @@ -179,4 +181,9 @@ void handleInviteCreated(@NotNull PartyInviteCreatedMessage message) {

this.updateNotifier.partyInviteCreated(party, senderId, invite.getSenderUsername(), targetUsername);
}

void handlePartyBroadcast(@NotNull PartyBroadcastMessage message) {
Party party = message.getParty();
this.updateNotifier.partyBroadcast(party);
}
}
14 changes: 14 additions & 0 deletions src/main/java/dev/emortal/velocity/party/cache/LocalParty.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.emortal.velocity.party.cache;

import dev.emortal.api.model.party.Party;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -21,4 +22,17 @@ public interface LocalParty {
@NotNull Collection<? extends LocalPartyMember> members();

@Nullable LocalPartyMember getMember(@NotNull UUID id);

default @NotNull Party toProto() {
Party.Builder builder = Party.newBuilder()
.setId(this.id())
.setLeaderId(this.leaderId().toString())
.setOpen(this.open());

for (LocalPartyMember member : this.members()) {
builder.addMembers(member.toProto());
}

return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.emortal.velocity.party.cache;

import dev.emortal.api.model.party.PartyMember;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;
Expand All @@ -9,4 +10,11 @@ public interface LocalPartyMember {
@NotNull UUID id();

@NotNull String username();

default @NotNull PartyMember toProto() {
return PartyMember.newBuilder()
.setId(this.id().toString())
.setUsername(this.username())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.mojang.brigadier.arguments.StringArgumentType;
import dev.emortal.api.service.party.PartyService;
import dev.emortal.api.utils.kafka.FriendlyKafkaProducer;
import dev.emortal.velocity.command.EmortalCommand;
import dev.emortal.velocity.lang.ChatMessages;
import dev.emortal.velocity.party.commands.subs.PartyBroadcastSub;
import dev.emortal.velocity.party.commands.subs.PartyCloseSub;
import dev.emortal.velocity.party.commands.subs.PartyDisbandSub;
import dev.emortal.velocity.party.commands.subs.PartyInviteSub;
Expand All @@ -22,7 +24,7 @@
public final class PartyCommand extends EmortalCommand {

public PartyCommand(@NotNull PartyService partyService, @NotNull PlayerResolver playerResolver,
@NotNull UsernameSuggesterProvider usernameSuggesterProvider) {
@NotNull UsernameSuggesterProvider usernameSuggesterProvider, @NotNull FriendlyKafkaProducer kafkaProducer) {
super("party", "p");

super.setPlayerOnly();
Expand All @@ -40,5 +42,8 @@ public PartyCommand(@NotNull PartyService partyService, @NotNull PlayerResolver
super.addSyntax(new PartyCloseSub(partyService), literal("close"));
super.addSyntax(new PartyListSub(partyService), literal("list"));
super.addSyntax(new PartySettingsSub(), literal("settings"));

super.addConditionalSyntax(source -> source.hasPermission("command.party.broadcast"),
new PartyBroadcastSub(kafkaProducer, partyService), literal("broadcast"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.emortal.velocity.party.commands.subs;

import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import dev.emortal.api.message.party.PartyBroadcastMessage;
import dev.emortal.api.model.party.Party;
import dev.emortal.api.service.party.PartyService;
import dev.emortal.api.utils.kafka.FriendlyKafkaProducer;
import dev.emortal.velocity.command.ArgumentProvider;
import dev.emortal.velocity.command.EmortalCommandExecutor;
import dev.emortal.velocity.lang.ChatMessages;
import org.jetbrains.annotations.NotNull;

public class PartyBroadcastSub implements EmortalCommandExecutor {
private final @NotNull FriendlyKafkaProducer kafkaProducer;
private final @NotNull PartyService partyService;

public PartyBroadcastSub(@NotNull FriendlyKafkaProducer kafkaProducer, @NotNull PartyService partyService) {
this.kafkaProducer = kafkaProducer;
this.partyService = partyService;
}

@Override
public void execute(@NotNull CommandSource source, @NotNull ArgumentProvider arguments) {
Player executor = (Player) source;

Party party = this.partyService.getPartyByPlayer(executor.getUniqueId());

if (party == null) return;

if (!party.getOpen()) {
ChatMessages.PARTY_BROADCAST_PARTY_CLOSED.send(executor);
return;
}

this.kafkaProducer.produceAndForget( PartyBroadcastMessage.newBuilder().setParty(party).build());
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package dev.emortal.velocity.party.notifier;

import com.velocitypowered.api.proxy.Player;
import dev.emortal.api.model.party.Party;
import dev.emortal.api.model.party.PartyMember;
import dev.emortal.velocity.adapter.player.PlayerProvider;
import dev.emortal.velocity.lang.ChatMessages;
import dev.emortal.velocity.party.cache.LocalParty;
import dev.emortal.velocity.adapter.player.PlayerProvider;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;

public final class ChatPartyUpdateNotifier implements PartyUpdateNotifier {
private static final Logger LOGGER = LoggerFactory.getLogger(ChatPartyUpdateNotifier.class);

private final @NotNull PlayerProvider playerProvider;

Expand Down Expand Up @@ -100,4 +105,19 @@ public void selfKicked(@NotNull UUID targetId, @NotNull String kickerName) {

ChatMessages.YOU_KICKED_FROM_PARTY.send(target, kickerName);
}

@Override
public void partyBroadcast(@NotNull Party party) {
String leaderId = party.getLeaderId();
PartyMember leader = party.getMembersList().stream()
.filter(member -> member.getId().equals(leaderId))
.findFirst().orElse(null);

if (leader == null) {
LOGGER.warn("Couldn't find leader for party {}", party.getId());
return;
}

this.playerProvider.allPlayers().forEach(player -> ChatMessages.PARTY_BROADCAST_MESSAGE.send(player, leader.getUsername()));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.emortal.velocity.party.notifier;

import dev.emortal.api.model.party.Party;
import dev.emortal.velocity.party.cache.LocalParty;
import org.jetbrains.annotations.NotNull;

Expand All @@ -26,4 +27,6 @@ public interface PartyUpdateNotifier {
void playerKicked(@NotNull LocalParty party, @NotNull String targetName, @NotNull String kickerName);

void selfKicked(@NotNull UUID targetId, @NotNull String kickerName);

void partyBroadcast(@NotNull Party party);
}

0 comments on commit 99fcccd

Please sign in to comment.