Skip to content

Commit

Permalink
Implement deserialization to notice composer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi committed Jun 18, 2023
1 parent d4fffee commit 94342f3
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,26 @@
import org.bukkit.SoundCategory;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.*;

import static com.eternalcode.core.notice.NoticeContent.*;

public class Notice {

private final List<Part<?>> parts = new ArrayList<>();
private final Map<NoticeType, Part<?>> parts = new LinkedHashMap<>();

private Notice(List<Part<?>> parts) {
this.parts.addAll(parts);
private Notice(Map<NoticeType, Part<?>> parts) {
this.parts.putAll(parts);
}

public List<Part<?>> parts() {
return Collections.unmodifiableList(this.parts);
return this.parts.values().stream()
.toList();
}

public static <T extends NoticeContent> Notice of(NoticeType<T> type, T content) {
public static <T extends NoticeContent> Notice of(NoticeType type, T content) {
return Notice.builder()
.with(new Part<>(type, content))
.withPart(new Part<>(type, content))
.build();
}

Expand Down Expand Up @@ -94,18 +92,18 @@ public static Notice sound(Sound sound, float volume, float pitch) {
}

public static Notice empty() {
return new Notice(Collections.emptyList());
return new Notice(Collections.emptyMap());
}

public static Builder builder() {
return new Builder();
}

public static class Builder {
private final List<Part<?>> parts = new ArrayList<>();
private final Map<NoticeType, Part<?>> parts = new LinkedHashMap<>();

Builder with(Part<?> part) {
this.parts.add(part);
Builder withPart(Part<?> part) {
this.parts.put(part.type, part);
return this;
}

Expand All @@ -114,48 +112,57 @@ public Notice build() {
}

public Builder chat(String... messages) {
return this.with(NoticeType.CHAT.of(new Text(List.of(messages))));
return this.chat(List.of(messages));
}

public Builder chat(Collection<String> messages) {
return this.with(NoticeType.CHAT.of(new Text(List.copyOf(messages))));
Part<?> removed = this.parts.remove(NoticeType.CHAT);
List<String> newMessages = new ArrayList<>();

if (removed != null && removed.content instanceof Text text) {
newMessages.addAll(text.messages());
}

newMessages.addAll(messages);

return this.withPart(new Part<>(NoticeType.CHAT, new Text(Collections.unmodifiableList(newMessages))));
}

public Builder actionBar(String message) {
return this.with(NoticeType.ACTION_BAR.of(new Text(List.of(message))));
return this.withPart(new Part<>(NoticeType.ACTION_BAR, new Text(List.of(message))));
}

public Builder title(String title) {
return this.with(NoticeType.TITLE.of(new Text(List.of(title))));
return this.withPart(new Part<>(NoticeType.TITLE, new Text(List.of(title))));
}

public Builder title(String title, String subtitle) {
return this.with(NoticeType.TITLE.of(new Text(List.of(title))))
.with(NoticeType.SUBTITLE.of(new Text(List.of(subtitle))));
return this.withPart(new Part<>(NoticeType.TITLE, new Text(List.of(title))))
.withPart(new Part<>(NoticeType.SUBTITLE, new Text(List.of(subtitle))));
}

public Builder subtitle(String subtitle) {
return this.with(NoticeType.SUBTITLE.of(new Text(List.of(subtitle))));
return this.withPart(new Part<>(NoticeType.SUBTITLE, new Text(List.of(subtitle))));
}

public Builder hideTitle() {
return this.with(NoticeType.TITLE_HIDE.of(None.INSTANCE));
return this.withPart(new Part<>(NoticeType.TITLE_HIDE, None.INSTANCE));
}

public Builder times(Duration in, Duration stay, Duration out) {
return this.with(NoticeType.TITLE_TIMES.of(new Times(in, stay, out)));
return this.withPart(new Part<>(NoticeType.TITLE_TIMES, new Times(in, stay, out)));
}

public Builder sound(Sound sound, float pitch, float volume) {
return this.with(NoticeType.SOUND.of(new Music(sound, null, pitch, volume)));
return this.withPart(new Part<>(NoticeType.SOUND, new Music(sound, null, pitch, volume)));
}

public Builder sound(Sound sound, SoundCategory category, float pitch, float volume) {
return this.with(NoticeType.SOUND.of(new Music(sound, category, pitch, volume)));
return this.withPart(new Part<>(NoticeType.SOUND, new Music(sound, category, pitch, volume)));
}

}

record Part<T extends NoticeContent>(NoticeType<T> type, T content) { }
record Part<T extends NoticeContent>(NoticeType type, T content) { }

}
Loading

0 comments on commit 94342f3

Please sign in to comment.