Skip to content

Commit

Permalink
fix(channel): add option to customize private channel config via prot…
Browse files Browse the repository at this point in the history
…otype
  • Loading branch information
Silthus committed Feb 22, 2022
1 parent 0d276b8 commit d2b2130
Show file tree
Hide file tree
Showing 33 changed files with 532 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void onEnable() {
super.onEnable();

if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
new PlaceholderApiIntegration(viewController()).init();
new PlaceholderApiIntegration(viewModule().replacements()).init();
logger().info("Enabled PlaceholderAPI integration.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@
import me.clip.placeholderapi.PlaceholderAPI;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.silthus.schat.ui.ViewController;
import net.silthus.schat.ui.Replacements;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class PlaceholderApiIntegration {

private final ViewController viewController;
private final Replacements replacements;

public PlaceholderApiIntegration(ViewController viewController) {
this.viewController = viewController;
public PlaceholderApiIntegration(Replacements replacements) {
this.replacements = replacements;
}

public void init() {
viewController.addMessageReplacement(message -> {
replacements.addMessageReplacement(message -> {
final Player player = Bukkit.getPlayer(message.source().uniqueId());
if (player != null)
return replacePlaceholderAPIPlaceholders(player);
Expand Down
56 changes: 56 additions & 0 deletions core/src/main/java/net/silthus/schat/channel/PrivateChannel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* This file is part of sChat, licensed under the MIT License.
* Copyright (C) Silthus <https://www.github.com/silthus>
* Copyright (C) sChat team and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.silthus.schat.channel;

import java.util.function.Consumer;
import lombok.Getter;
import lombok.experimental.Accessors;
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.policies.JoinChannelPolicy;

import static net.silthus.schat.channel.ChannelSettings.GLOBAL;
import static net.silthus.schat.channel.ChannelSettings.HIDDEN;
import static net.silthus.schat.channel.ChannelSettings.PRIVATE;
import static net.silthus.schat.channel.ChannelSettings.PROTECTED;

@Accessors(fluent = true)
public final class PrivateChannel {

private static final Consumer<Channel.Builder> DEFAULTS = builder -> builder
.set(GLOBAL, true)
.set(PRIVATE, true)
.set(HIDDEN, true)
.set(PROTECTED, true)
.policy(JoinChannelPolicy.class, Chatter::isJoined);

@Getter
private static Consumer<Channel.Builder> prototype = DEFAULTS;

public static void configure(Consumer<Channel.Builder> channel) {
prototype = prototype.andThen(channel);
}

private PrivateChannel() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,25 @@
package net.silthus.schat.commands;

import java.util.Set;
import java.util.function.Function;
import java.util.function.Consumer;
import java.util.function.Predicate;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.experimental.Accessors;
import net.kyori.adventure.text.Component;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.channel.ChannelRepository;
import net.silthus.schat.channel.PrivateChannel;
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.command.Command;
import net.silthus.schat.command.CommandBuilder;
import net.silthus.schat.messenger.Messenger;
import net.silthus.schat.policies.JoinChannelPolicy;
import org.jetbrains.annotations.NotNull;

import static net.kyori.adventure.text.Component.text;
import static net.silthus.schat.channel.ChannelSettings.GLOBAL;
import static net.silthus.schat.channel.ChannelSettings.HIDDEN;
import static net.silthus.schat.channel.ChannelSettings.PRIVATE;
import static net.silthus.schat.channel.ChannelSettings.PROTECTED;

/**
* Creates a new private channel between the two chatters.
Expand All @@ -56,29 +54,35 @@
public class CreatePrivateChannelCommand implements Command {

private static final @NonNull Predicate<Channel> IS_PRIVATE = channel -> channel.is(PRIVATE);
@Getter(AccessLevel.PROTECTED)
private static @NonNull Consumer<CreatePrivateChannelCommand.Builder> prototype = builder -> builder.channelSettings(PrivateChannel.prototype());

@Getter
@Setter
private static @NonNull Function<CreatePrivateChannelCommand.Builder, CreatePrivateChannelCommand.Builder> prototype = builder -> builder;
public static void prototype(@NonNull Consumer<Builder> consumer) {
prototype = prototype.andThen(consumer);
}

public static Result createPrivateChannel(Chatter source, Chatter target) {
return createPrivateChannelBuilder(source, target).create().execute();
}

public static Builder createPrivateChannelBuilder(Chatter source, Chatter target) {
return prototype.apply(new Builder(source, target));
final Builder builder = new Builder(source, target);
prototype().accept(builder);
return builder;
}

private final @NonNull Chatter source;
private final @NonNull Chatter target;
private final @NonNull ChannelRepository repository;
private final @NonNull Messenger messenger;
private final @NonNull Consumer<Channel.Builder> channelSettings;

protected CreatePrivateChannelCommand(Builder builder) {
this.source = builder.source;
this.target = builder.target;
this.repository = builder.channelRepository;
this.messenger = builder.messenger;
this.channelSettings = builder.channelSettings;
}

@Override
Expand Down Expand Up @@ -110,14 +114,10 @@ private String privateChannelKey() {
}

private Channel createPrivateChannel(String key, Component name) {
final Channel channel = Channel.channel(key)
.name(name)
.set(GLOBAL, true)
.set(PRIVATE, true)
.set(HIDDEN, true)
.set(PROTECTED, true)
.policy(JoinChannelPolicy.class, Chatter::isJoined)
.create();
final Channel.Builder builder = Channel.channel(key).name(name);
channelSettings().accept(builder);

final Channel channel = builder.create();
repository.add(channel);
return channel;
}
Expand All @@ -136,6 +136,7 @@ public static class Builder extends CommandBuilder<Builder, CreatePrivateChannel

private final Chatter source;
private final Chatter target;
private Consumer<Channel.Builder> channelSettings = builder -> {};
private ChannelRepository channelRepository;
private Messenger messenger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package net.silthus.schat.commands;

import java.io.Serial;
import java.util.function.Function;
import java.util.function.Consumer;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
Expand All @@ -47,15 +47,20 @@
public class JoinChannelCommand implements Command {

@Getter
@Setter
private static @NonNull Function<Builder, Builder> prototype = builder -> builder;
private static @NonNull Consumer<Builder> prototype = builder -> {};

public static void prototype(Consumer<Builder> consumer) {
prototype = prototype().andThen(consumer);
}

public static Result joinChannel(Chatter chatter, Channel channel) {
return joinChannelBuilder(chatter, channel).create().execute();
}

public static Builder joinChannelBuilder(Chatter chatter, Channel channel) {
return prototype().apply(new Builder(chatter, channel));
final Builder builder = new Builder(chatter, channel);
prototype().accept(builder);
return builder;
}

private final @NonNull Chatter chatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
package net.silthus.schat.commands;

import java.util.function.Function;
import java.util.function.Consumer;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
Expand All @@ -47,15 +47,20 @@
public class LeaveChannelCommand implements Command {

@Getter
@Setter
private static @NonNull Function<LeaveChannelCommand.Builder, LeaveChannelCommand.Builder> prototype = builder -> builder;
private static @NonNull Consumer<Builder> prototype = builder -> {};

public static void prototype(Consumer<Builder> consumer) {
prototype = prototype().andThen(consumer);
}

public static Result leaveChannel(Chatter chatter, Channel channel) {
return leaveChannelBuilder(chatter, channel).execute();
}

public static LeaveChannelCommand.Builder leaveChannelBuilder(Chatter chatter, Channel channel) {
return prototype().apply(new LeaveChannelCommand.Builder(chatter, channel));
final Builder builder = new Builder(chatter, channel);
prototype().accept(builder);
return builder;
}

private final @NonNull Chatter chatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
package net.silthus.schat.commands;

import java.util.function.Function;
import java.util.function.Consumer;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
Expand All @@ -45,15 +45,20 @@
public class SendMessageCommand implements Command {

@Getter
@Setter
private static @NonNull Function<SendMessageCommand.Builder, SendMessageCommand.Builder> prototype = builder -> builder;
private static @NonNull Consumer<Builder> prototype = builder -> {};

public static void prototype(Consumer<Builder> consumer) {
prototype = prototype().andThen(consumer);
}

public static SendMessageResult sendMessage(Message message) {
return sendMessageBuilder(message).create().execute();
}

public static Builder sendMessageBuilder(Message message) {
return prototype().apply(new Builder(message));
final Builder builder = new Builder(message);
prototype().accept(builder);
return builder;
}

private final @NonNull Message message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
package net.silthus.schat.commands;

import java.util.function.Function;
import java.util.function.Consumer;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
Expand All @@ -42,15 +42,20 @@
public class SendPrivateMessageCommand implements Command {

@Getter
@Setter
private static @NonNull Function<SendPrivateMessageCommand.Builder, SendPrivateMessageCommand.Builder> prototype = builder -> builder;
private static @NonNull Consumer<Builder> prototype = builder -> {};

public static void prototype(Consumer<Builder> consumer) {
prototype = prototype().andThen(consumer);
}

public static SendMessageResult sendPrivateMessage(Chatter source, Chatter target, Component text) {
return sendPrivateMessageBuilder(source, target, text).create().execute();
}

public static Builder sendPrivateMessageBuilder(Chatter source, Chatter target, Component text) {
return prototype.apply(new Builder(source, target, text));
final Builder builder = new Builder(source, target, text);
prototype().accept(builder);
return builder;
}

private final @NonNull Chatter source;
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/net/silthus/schat/pointer/Pointered.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,21 @@ public interface Pointered {
*
* @param pointer the pointer to resolve
* @return the value of the pointer. false if it does not exist.
* @since next
*/
default boolean is(Pointer<Boolean> pointer) {
return getOrDefault(pointer, false);
}

/**
* Tries to resolve the value of the boolean pointer and returns the opposite.
*
* <p>Returns {@code false} if the pointer does not exist.</p>
*
* @param pointer the pointer to resolve
* @return the negated value of the pointer. true if it does not exist.
* @since next
*/
default boolean isNot(Pointer<Boolean> pointer) {
return !is(pointer);
}
Expand Down
4 changes: 0 additions & 4 deletions core/src/main/java/net/silthus/schat/pointer/Setting.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ public sealed interface Setting<V> extends Pointer<V> permits SettingImpl {
return new SettingImpl<>(type, key, defaultValue);
}

static @NotNull Setting<Settings> settings(final @NonNull String key, final @NonNull Settings.Builder settingsBuilder) {
return new SettingImpl<>(Settings.class, key, settingsBuilder::create);
}

/**
* The default value of the setting that is used if the setting is not set.
*
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/net/silthus/schat/pointer/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ public sealed interface Settings extends Pointers permits SettingsImpl {
*/
@NotNull Builder toBuilder();

/**
* Copies the settings of the given settings container into this container.
*
* <p>Any settings in this container that exist in the given container will be overwritten.</p>
*
* @param settings the settings to copy
* @return this settings
* @since next
*/
@NotNull Settings copyFrom(Settings settings);

/**
* A builder of settings.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ public <T> boolean contains(@NonNull Pointer<T> pointer) {
return new BuilderImpl(this);
}

@Override
public @NotNull SettingsImpl copyFrom(Settings settings) {
SettingsImpl s = (SettingsImpl) settings;
unknowns.putAll(s.unknowns);
pointers.putAll(s.pointers);
return this;
}

@Override
public String toString() {
final HashMap<String, Object> keyValueMap = new HashMap<>();
Expand Down
Loading

0 comments on commit d2b2130

Please sign in to comment.