Skip to content

Commit

Permalink
fix: NPE clearing caches via plugin message, close #92
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Apr 22, 2024
1 parent 6eb7bd2 commit f0968a2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ default void notifyAllSignModerators(@NotNull SignWrite write) {
notifyLocalSignModerators(write);
getPlugin().getBroker().ifPresent(broker -> Message.builder()
.type(Message.MessageType.SIGN_WRITE)
.target(Message.TARGET_ALL, Message.TargetType.PLAYER)
.payload(Payload.signEdit(write))
.build().send(broker, (OnlineUser) write.getEditor()));
.target(Message.TARGET_ALL, Message.TargetType.PLAYER).build()
.send(broker, (OnlineUser) write.getEditor()));
}

private void notifySignModerator(@NotNull OnlineUser moderator, @NotNull SignWrite write) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import net.william278.huskclaims.user.OnlineUser;
Expand Down Expand Up @@ -57,11 +58,12 @@ public class Message {
@SerializedName("source_server")
private String sourceServer;

private Message(@NotNull MessageType type, @NotNull String target, @NotNull TargetType targetType, @NotNull Payload payload) {
private Message(@NotNull MessageType type, @NotNull String target, @NotNull TargetType targetType,
@NotNull Payload payload) {
this.type = type;
this.target = target;
this.payload = payload;
this.targetType = targetType;
this.payload = payload;
}

@NotNull
Expand All @@ -78,15 +80,13 @@ public void send(@NotNull Broker broker, @NotNull OnlineUser sender) {
/**
* Builder for {@link Message}s
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class Builder {
private MessageType type;
private Payload payload = Payload.empty();
private TargetType targetType = TargetType.PLAYER;
private String target;

private Builder() {
}

@NotNull
public Builder type(@NotNull MessageType type) {
this.type = type;
Expand All @@ -108,6 +108,9 @@ public Builder target(@NotNull String target, @NotNull TargetType targetType) {

@NotNull
public Message build() {
if (target == null || type == null) {
throw new IllegalStateException("Message not fully built. Type: " + type + ", Target: " + target);
}
return new Message(type, target, targetType, payload);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ default void handleRequestUserList(@NotNull Message message, @Nullable OnlineUse

Message.builder()
.type(Message.MessageType.UPDATE_USER_LIST)
.target(message.getSourceServer(), Message.TargetType.SERVER)
.payload(Payload.userList(getPlugin().getOnlineUsers().stream().map(online -> (User) online).toList()))
.build().send(getBroker(), receiver);
.target(message.getSourceServer(), Message.TargetType.SERVER).build()
.send(getBroker(), receiver);
}

// Handle inbound user list updates (returned from requests)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ default boolean deleteUserGroup(@NotNull OnlineUser owner, @NotNull String group
private void publishGroupChange(@NotNull OnlineUser user) {
getPlugin().getBroker().ifPresent(broker -> Message.builder()
.type(Message.MessageType.INVALIDATE_USER_GROUPS)
.payload(Payload.uuid(user.getUuid()))
.target(Message.TARGET_ALL, Message.TargetType.SERVER)
.payload(Payload.uuid(user.getUuid()))
.build().send(broker, user));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void editSavedUser(@NotNull SavedUser user, @NotNull Consumer<SavedUser>
sender -> Message.builder()
.type(Message.MessageType.INVALIDATE_USER_CACHE)
.payload(Payload.uuid(uuid))
.build()
.target(Message.TARGET_ALL, Message.TargetType.SERVER).build()
.send(broker, sender))
);
}
Expand Down

0 comments on commit f0968a2

Please sign in to comment.