Skip to content

Commit

Permalink
refactor: fire chatter events from inside chatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Feb 21, 2022
1 parent a760b1d commit a25b331
Show file tree
Hide file tree
Showing 50 changed files with 395 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public void sendMessage(Chatter chatter) {

@Then("{chatter} receives the message")
public void receiveMessage(Chatter chatter) {
assertThat(chatter.lastMessage()).isPresent()
.get().extracting(Message::text)
assertThat(chatter.messages().last()).isNotNull()
.extracting(Message::text)
.isEqualTo(context.lastMessageText());
}

@Then("{chatter} does not receive a message")
public void playerDoesNotReceiveAMessage(Chatter chatter) {
assertThat(chatter.lastMessage()).isEmpty();
assertThat(chatter.messages().last()).isNull();
}

@Then("{chatter} am/is a member of the {channel} channel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import net.silthus.schat.channel.Channel;
import net.silthus.schat.cucumber.models.User;
import net.silthus.schat.platform.sender.SenderMock;
import net.silthus.schat.ui.view.View;
import net.silthus.schat.ui.View;

import static net.kyori.adventure.text.Component.text;
import static net.silthus.schat.identity.Identity.identity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
import lombok.experimental.Accessors;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.message.Message;
import net.silthus.schat.platform.sender.SenderMock;
import net.silthus.schat.ui.view.View;
import net.silthus.schat.ui.View;

@Getter
@Setter
Expand Down Expand Up @@ -86,10 +85,6 @@ public View view() {
return server().plugin().viewProvider().view(chatter());
}

public Message lastMessage() {
return chatter().lastMessage().orElse(null);
}

@Override
public String toString() {
return name + "@" + server();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import net.silthus.schat.platform.messaging.GatewayProviderRegistry;
import net.silthus.schat.platform.plugin.AbstractSChatServerPlugin;
import net.silthus.schat.platform.sender.Sender;
import net.silthus.schat.ui.view.ViewProvider;
import net.silthus.schat.ui.ViewProvider;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.identity.Identity;
import net.silthus.schat.platform.chatter.AbstractChatterFactory;
import net.silthus.schat.ui.DynamicViewConnector;
import net.silthus.schat.ui.ViewConnector;
import net.silthus.schat.ui.view.DynamicViewConnector;
import net.silthus.schat.ui.view.ViewProvider;
import net.silthus.schat.ui.ViewProvider;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.chatter.ChatterRepository;
import net.silthus.schat.message.Message;
import net.silthus.schat.ui.view.View;
import net.silthus.schat.ui.view.ViewProvider;
import net.silthus.schat.ui.View;
import net.silthus.schat.ui.ViewProvider;
import org.bukkit.plugin.Plugin;

import static net.silthus.schat.message.Message.message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package net.silthus.schat.bukkit.adapter;

import be.seeseemelk.mockbukkit.entity.PlayerMock;
import java.util.Optional;
import lombok.SneakyThrows;
import net.kyori.adventure.text.Component;
import net.silthus.schat.bukkit.BukkitTests;
Expand All @@ -49,15 +50,17 @@ class PlayerChatListenerTest extends BukkitTests {
@BeforeEach
void setUp() {
player = server.addPlayer();
chatter = Chatter.chatter(identity(player))
chatter = Chatter.chatterBuilder(identity(player))
.viewConnector(c -> () -> handleMessage(c))
.create();
PlayerChatListener listener = new PlayerChatListener(createInMemoryChatterRepository());
server.getPluginManager().registerEvents(listener, mockPlugin);
}

private void handleMessage(Chatter chatter) {
lastMessage = chatter.lastMessage().map(Message::text).orElse(null);
lastMessage = Optional.ofNullable(chatter.messages().last())
.map(Message::text)
.orElse(null);
}

private void chat() {
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/net/silthus/schat/channel/ChannelPrototype.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,25 @@
package net.silthus.schat.channel;

import net.silthus.schat.eventbus.EventBus;
import org.jetbrains.annotations.ApiStatus;

/**
* Internal utility for configuring the prototype of channels.
*
* @since next
*/
@ApiStatus.Internal
public final class ChannelPrototype {

/**
* Configures the prototype for creating new channels.
*
* <p>This is internal functionality and should not be used.</p>
*
* @param eventBus the event bus to use
* @since next
*/
@ApiStatus.Internal
public static void configure(EventBus eventBus) {
ChannelImpl.prototype(builder -> builder.eventBus(eventBus));
}
Expand Down
59 changes: 51 additions & 8 deletions core/src/main/java/net/silthus/schat/chatter/Chatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import lombok.NonNull;
import net.kyori.adventure.text.Component;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.eventbus.EventBus;
import net.silthus.schat.identity.Identified;
import net.silthus.schat.identity.Identity;
import net.silthus.schat.message.Message;
import net.silthus.schat.message.MessageTarget;
import net.silthus.schat.message.Messages;
import net.silthus.schat.pointer.Pointer;
import net.silthus.schat.repository.Entity;
import net.silthus.schat.ui.ViewConnector;
import net.silthus.schat.util.Permissable;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
Expand All @@ -55,11 +57,25 @@ static Chatter empty() {
return ChatterImpl.EMPTY;
}

static Chatter createChatter(@NonNull Identity identity) {
return chatter(identity).create();
/**
* Creates a new chatter for the given identity.
*
* @param identity the identity of the chatter
* @return the created chatter
* @since next
*/
static Chatter chatter(@NonNull Identity identity) {
return chatterBuilder(identity).create();
}

static Builder chatter(@NonNull Identity identity) {
/**
* Creates a new chatter builder that allows modifying details of the underlying chatter implementation.
*
* @param identity the identity of the chatter
* @return the builder
* @since next
*/
static Builder chatterBuilder(@NonNull Identity identity) {
return ChatterImpl.builder(identity);
}

Expand All @@ -84,11 +100,9 @@ default boolean isActiveChannel(@Nullable Channel channel) {

boolean isJoined(@Nullable Channel channel);

void leave(Channel channel);

@NotNull @Unmodifiable Set<Message> messages();
void leave(@NonNull Channel channel);

Optional<Message> lastMessage();
@NotNull @Unmodifiable Messages messages();

void updateView();

Expand All @@ -101,10 +115,39 @@ default Message.Draft message(Component text) {
}

interface Builder {
/**
* Sets the event bus to use by this chatter.
*
* <p>This should be set for all chatters using the {@link ChatterPrototype}.</p>
*
* @param eventBus the event bus
* @return this builder
* @since next
*/
@ApiStatus.Internal
@NotNull Builder eventBus(EventBus eventBus);

@ApiStatus.Internal
@NotNull Builder viewConnector(@NonNull ViewConnector.Factory viewConnectorFactory);

/**
* Sets the permission handler used by this chatter.
*
* <p>This should be done by the {@link ChatterFactory} of the implementing platform.</p>
*
* @param permissionHandler the permission handler
* @return this builder
* @since next
*/
@ApiStatus.Internal
@NotNull Builder permissionHandler(@NonNull PermissionHandler permissionHandler);

/**
* Creates the chatter.
*
* @return the newly created chatter
* @since next
*/
@NotNull Chatter create();
}

Expand Down
Loading

0 comments on commit a25b331

Please sign in to comment.