Begin API abstraction!

This commit is contained in:
Draycia committed 2020-09-25 01:43:16 -07:00
1 parent 59511c037f
commit 7299ff64a5
86 files changed
+399 -436

No files matched your search

@@ -0,0 +1,4 @@
package net.draycia.carbon.api;
public class CarbonChat {
}
@@ -0,0 +1,95 @@
package net.draycia.carbon.api;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.List;
public class Context {
private @NonNull final String key;
private @NonNull final Object value;
public Context(@NonNull final String key, @NonNull final Object value) {
this.key = key;
this.value = value;
}
public @NonNull String key() {
return this.key;
}
public @NonNull Object value() {
return this.value;
}
public boolean isFloat() {
return this.value instanceof Float;
}
public boolean isDouble() {
return this.value() instanceof Double;
}
public boolean isByte() {
return this.value() instanceof Byte;
}
public boolean isInteger() {
return this.value() instanceof Integer;
}
public boolean isLong() {
return this.value() instanceof Long;
}
public boolean isString() {
return this.value() instanceof String;
}
public boolean isBoolean() {
return this.value() instanceof Boolean;
}
public boolean isList() {
return this.value() instanceof List;
}
public boolean isListChecked(final Class<?> type) {
if (!this.isList()) {
return false;
}
final List<?> list = this.asList();
if (list.size() > 0) {
final Object value = list.get(0);
if (value != null) {
return list.get(0).getClass().isAssignableFrom(type);
}
}
return false;
}
public boolean isNumber() {
return this.value() instanceof Number;
}
public String asString() {
return (String) this.value();
}
public Boolean asBoolean() {
return (Boolean) this.value();
}
public <T> List<T> asList() {
return (List<T>) this.value();
}
public Number asNumber() {
return (Number) this.value();
}
}
@@ -0,0 +1,109 @@
package net.draycia.carbon.api.channels;
import net.draycia.carbon.api.Context;
import net.draycia.carbon.api.users.ChatUser;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;
public abstract class ChatChannel implements ForwardingAudience {
@NonNull
public abstract List<ChatUser> audiences();
@Nullable
public abstract TextColor channelColor(@NonNull ChatUser user);
@Nullable
public abstract String format(@NonNull String group);
public abstract boolean isDefault();
public abstract boolean ignorable();
public abstract boolean crossServer();
@NonNull
public abstract String name();
@NonNull
public abstract String key();
@Nullable
public abstract String messagePrefix();
@Nullable
public abstract String aliases();
@Nullable
public abstract String switchMessage();
@Nullable
public abstract String switchOtherMessage();
@Nullable
public abstract String switchFailureMessage();
@Nullable
public abstract String cannotIgnoreMessage();
@Nullable
public abstract String toggleOffMessage();
@Nullable
public abstract String toggleOnMessage();
@Nullable
public abstract String toggleOtherOnMessage();
@Nullable
public abstract String toggleOtherOffMessage();
@Nullable
public abstract String cannotUseMessage();
public abstract boolean primaryGroupOnly();
public abstract boolean honorsRecipientList();
public abstract boolean permissionGroupMatching();
public abstract boolean shouldCancelChatEvent();
public abstract boolean testContext(@NonNull ChatUser sender, @NonNull ChatUser target);
@NonNull
public abstract List<@NonNull Pattern> itemLinkPatterns();
@Nullable
public abstract Context context(@NonNull String key);
@NonNull
public abstract List<@NonNull String> groupOverrides();
public abstract boolean canPlayerUse(@NonNull ChatUser user);
public abstract boolean canPlayerSee(@NonNull ChatUser sender, @NonNull ChatUser target, boolean checkSpying);
public abstract boolean canPlayerSee(@NonNull ChatUser target, boolean checkSpying);
@NonNull
public abstract Component sendMessage(@NonNull ChatUser user, @NonNull String message, boolean fromBungee);
@NonNull
public abstract Component sendMessage(@NonNull ChatUser user, @NonNull Collection<@NonNull ChatUser> recipients, @NonNull String message, boolean fromBungee);
public abstract void sendComponent(@NonNull ChatUser user, @NonNull Component component);
@Nullable
public String processPlaceholders(@NonNull final ChatUser user, @Nullable final String input) {
return input;
}
}
@@ -0,0 +1,35 @@
package net.draycia.carbon.api.commands;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.List;
public class CommandSettings {
private final boolean enabled;
@NonNull
private final String name;
@NonNull
private final List<@NonNull String> aliases;
public CommandSettings(final boolean enabled, @NonNull final String name, @NonNull final List<@NonNull String> aliases) {
this.enabled = enabled;
this.name = name;
this.aliases = aliases;
}
public @NonNull String @NonNull [] aliases() {
return this.aliases.toArray(new String[0]);
}
@NonNull
public String name() {
return this.name;
}
public boolean enabled() {
return this.enabled;
}
}
@@ -0,0 +1,31 @@
package net.draycia.carbon.api.events;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.events.misc.CarbonEvent;
import net.kyori.registry.Registry;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.List;
public class ChannelRegisterEvent implements CarbonEvent {
@NonNull
private final List<@NonNull ChatChannel> registeredChannels;
@NonNull
private final Registry<String, ChatChannel> registry;
public ChannelRegisterEvent(@NonNull final List<@NonNull ChatChannel> registeredChannels, @NonNull final Registry<String, ChatChannel> registry) {
this.registeredChannels = registeredChannels;
this.registry = registry;
}
public void register(@NonNull final ChatChannel chatChannel) {
this.registry.register(chatChannel.key(), chatChannel);
}
@NonNull
public List<@NonNull ChatChannel> registeredChannels() {
return this.registeredChannels;
}
}
@@ -0,0 +1,55 @@
package net.draycia.carbon.api.events;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.ChatUser;
import net.draycia.carbon.api.events.misc.CarbonEvent;
import net.kyori.event.Cancellable;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
public class ChannelSwitchEvent implements CarbonEvent, Cancellable {
@NonNull
private final ChatChannel channel;
@NonNull
private final ChatUser user;
@Nullable
private String failureMessage;
private boolean cancelled = false;
public ChannelSwitchEvent(@NonNull final ChatChannel channel, @NonNull final ChatUser user, @Nullable final String failureMessage) {
this.channel = channel;
this.user = user;
this.failureMessage = failureMessage;
}
@NonNull
public ChatUser user() {
return this.user;
}
@NonNull
public ChatChannel channel() {
return this.channel;
}
@Nullable
public String failureMessage() {
return this.failureMessage;
}
public void failureMessage(@Nullable final String failureMessage) {
this.failureMessage = failureMessage;
}
@Override
public boolean cancelled() {
return this.cancelled;
}
@Override
public void cancelled(final boolean cancelled) {
this.cancelled = cancelled;
}
}
@@ -0,0 +1,79 @@
package net.draycia.carbon.api.events;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.ChatUser;
import net.draycia.carbon.api.events.misc.CarbonEvent;
import net.kyori.adventure.text.TextComponent;
import net.kyori.event.Cancellable;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
public class ChatComponentEvent implements CarbonEvent, Cancellable {
private boolean isCancelled = false;
@NonNull
private final ChatUser sender;
@Nullable
private final ChatUser target;
@NonNull
private ChatChannel chatChannel;
@NonNull
private TextComponent component;
@NonNull
private final String originalMessage;
public ChatComponentEvent(@NonNull final ChatUser sender, @Nullable final ChatUser target,
@NonNull final ChatChannel chatChannel, @NonNull final TextComponent component,
@NonNull final String originalMessage) {
this.sender = sender;
this.target = target;
this.chatChannel = chatChannel;
this.component = component;
this.originalMessage = originalMessage;
}
@Override
public boolean cancelled() {
return this.isCancelled;
}
@Override
public void cancelled(final boolean cancelled) {
this.isCancelled = cancelled;
}
@NonNull
public ChatUser sender() {
return this.sender;
}
@Nullable
public ChatUser target() {
return this.target;
}
@NonNull
public ChatChannel channel() {
return this.chatChannel;
}
public void channel(@NonNull final ChatChannel chatChannel) {
this.chatChannel = chatChannel;
}
@NonNull
public TextComponent component() {
return this.component;
}
public void component(@NonNull final TextComponent component) {
this.component = component;
}
@NonNull
public String originalMessage() {
return this.originalMessage;
}
}
@@ -0,0 +1,82 @@
package net.draycia.carbon.api.events;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.ChatUser;
import net.draycia.carbon.api.events.misc.CarbonEvent;
import net.kyori.event.Cancellable;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
public class ChatFormatEvent implements CarbonEvent, Cancellable {
private boolean isCancelled = false;
@NonNull
private final ChatUser sender;
@Nullable
private final ChatUser target;
@NonNull
private ChatChannel chatChannel;
@Nullable
private String format;
@NonNull
private String message;
public ChatFormatEvent(@NonNull final ChatUser sender, @Nullable final ChatUser target,
@NonNull final ChatChannel chatChannel, @Nullable final String format,
@NonNull final String message) {
this.sender = sender;
this.target = target;
this.chatChannel = chatChannel;
this.format = format;
this.message = message;
}
@Override
public boolean cancelled() {
return this.isCancelled;
}
@Override
public void cancelled(final boolean cancelled) {
this.isCancelled = cancelled;
}
@NonNull
public ChatUser sender() {
return this.sender;
}
@Nullable
public ChatUser target() {
return this.target;
}
@NonNull
public ChatChannel channel() {
return this.chatChannel;
}
public void channel(@NonNull final ChatChannel chatChannel) {
this.chatChannel = chatChannel;
}
@Nullable
public String format() {
return this.format;
}
public void format(@Nullable final String format) {
this.format = format;
}
@NonNull
public String message() {
return this.message;
}
public void message(@NonNull final String message) {
this.message = message;
}
}
@@ -0,0 +1,43 @@
package net.draycia.carbon.api.events;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.ChatUser;
import net.draycia.carbon.api.Context;
import net.draycia.carbon.api.events.misc.CarbonEvent;
import net.kyori.event.Cancellable;
import org.checkerframework.checker.nullness.qual.NonNull;
public class MessageContextEvent implements CarbonEvent, Cancellable {
private @NonNull final ChatChannel channel;
private @NonNull final ChatUser user;
private boolean cancelled = false;
public MessageContextEvent(final @NonNull ChatChannel channel, final @NonNull ChatUser user) {
this.channel = channel;
this.user = user;
}
@Override
public boolean cancelled() {
return this.cancelled;
}
@Override
public void cancelled(final boolean cancelled) {
this.cancelled = cancelled;
}
public Context context(@NonNull final String key) {
return this.channel.context(key);
}
public @NonNull ChatChannel channel() {
return this.channel;
}
public @NonNull ChatUser user() {
return this.user;
}
}
@@ -0,0 +1,73 @@
package net.draycia.carbon.api.events;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.ChatUser;
import net.draycia.carbon.api.events.misc.CarbonEvent;
import net.kyori.event.Cancellable;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
public class PreChatFormatEvent implements CarbonEvent, Cancellable {
private boolean isCancelled = false;
@NonNull
private final ChatUser user;
@NonNull
private ChatChannel chatChannel;
@Nullable
private String format;
@NonNull
private String message;
public PreChatFormatEvent(@NonNull final ChatUser user, @NonNull final ChatChannel chatChannel,
@Nullable final String format, @NonNull final String message) {
this.user = user;
this.chatChannel = chatChannel;
this.format = format;
this.message = message;
}
@Override
public boolean cancelled() {
return this.isCancelled;
}
@Override
public void cancelled(final boolean cancelled) {
this.isCancelled = cancelled;
}
@NonNull
public ChatUser user() {
return this.user;
}
@NonNull
public ChatChannel channel() {
return this.chatChannel;
}
public void channel(@NonNull final ChatChannel chatChannel) {
this.chatChannel = chatChannel;
}
@Nullable
public String format() {
return this.format;
}
public void format(@Nullable final String format) {
this.format = format;
}
@NonNull
public String message() {
return this.message;
}
public void message(@NonNull final String message) {
this.message = message;
}
}
@@ -0,0 +1,69 @@
package net.draycia.carbon.api.events;
import net.draycia.carbon.api.users.ChatUser;
import net.draycia.carbon.api.events.misc.CarbonEvent;
import net.kyori.adventure.text.Component;
import net.kyori.event.Cancellable;
import org.checkerframework.checker.nullness.qual.NonNull;
public class PrivateMessageEvent implements CarbonEvent, Cancellable {
@NonNull
private final ChatUser sender;
@NonNull
private final ChatUser target;
@NonNull
private final Component senderComponent;
@NonNull
private final Component targetComponent;
@NonNull
private final String message;
private boolean cancelled = false;
public PrivateMessageEvent(@NonNull final ChatUser sender, @NonNull final ChatUser target,
@NonNull final Component senderComponent, @NonNull final Component targetComponent,
@NonNull final String message) {
this.sender = sender;
this.target = target;
this.senderComponent = senderComponent;
this.targetComponent = targetComponent;
this.message = message;
}
@Override
public boolean cancelled() {
return this.cancelled;
}
@Override
public void cancelled(final boolean cancelled) {
this.cancelled = cancelled;
}
@NonNull
public ChatUser sender() {
return this.sender;
}
@NonNull
public ChatUser target() {
return this.target;
}
@NonNull
public Component senderComponent() {
return this.senderComponent;
}
@NonNull
public Component targetComponent() {
return this.targetComponent;
}
@NonNull
public String message() {
return this.message;
}
}
@@ -0,0 +1,48 @@
package net.draycia.carbon.api.events;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.ChatUser;
import net.draycia.carbon.api.Context;
import net.draycia.carbon.api.events.misc.CarbonEvent;
import net.kyori.event.Cancellable;
import org.checkerframework.checker.nullness.qual.NonNull;
public class ReceiverContextEvent implements CarbonEvent, Cancellable {
private @NonNull final ChatChannel channel;
private @NonNull final ChatUser sender;
private @NonNull final ChatUser recipient;
private boolean cancelled = false;
public ReceiverContextEvent(final @NonNull ChatChannel channel, final @NonNull ChatUser sender, final @NonNull ChatUser recipient) {
this.channel = channel;
this.sender = sender;
this.recipient = recipient;
}
public Context context(@NonNull final String key) {
return this.channel.context(key);
}
@Override
public boolean cancelled() {
return this.cancelled;
}
@Override
public void cancelled(final boolean cancelled) {
this.cancelled = cancelled;
}
public @NonNull ChatChannel channel() {
return this.channel;
}
public @NonNull ChatUser sender() {
return this.sender;
}
public @NonNull ChatUser recipient() {
return this.recipient;
}
}
@@ -0,0 +1,5 @@
package net.draycia.carbon.api.events.misc;
public interface CarbonEvent {
}
@@ -0,0 +1,48 @@
package net.draycia.carbon.api.events.misc;
import net.kyori.event.EventSubscriber;
import net.kyori.event.PostResult;
import net.kyori.event.SimpleEventBus;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.function.Consumer;
public final class CarbonEvents {
private CarbonEvents() {
}
private static final SimpleEventBus<CarbonEvent> EVENT_BUS = new SimpleEventBus<>(CarbonEvent.class);
public static <T extends CarbonEvent> void register(@NonNull final Class<T> clazz, @NonNull final EventSubscriber<T> subscriber) {
EVENT_BUS.register(clazz, subscriber);
}
public static <T extends CarbonEvent> void register(@NonNull final Class<T> clazz, final int priority, final boolean consumeCancelled, @NonNull final Consumer<T> consumer) {
EVENT_BUS.register(clazz, new EventSubscriber<T>() {
@Override
public int postOrder() {
return priority;
}
@Override
public boolean consumeCancelledEvents() {
return consumeCancelled;
}
@Override
public void invoke(@NonNull final T event) {
consumer.accept(event);
}
});
}
public static void unregister(@NonNull final EventSubscriber<? super CarbonEvent> subscriber) {
EVENT_BUS.unregister(subscriber);
}
public static PostResult post(@NonNull final CarbonEvent event) {
return EVENT_BUS.post(event);
}
}
@@ -0,0 +1,22 @@
package net.draycia.carbon.api.messaging;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import net.draycia.carbon.api.users.ChatUser;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public interface MessageService {
void registerUserMessageListener(@NonNull final String key, @NonNull final BiConsumer<@NonNull ChatUser, @NonNull ByteArrayDataInput> listener);
void registerUUIDMessageListener(@NonNull final String key, @NonNull final BiConsumer<@NonNull UUID, @NonNull ByteArrayDataInput> listener);
void unregisterMessageListener(@NonNull final String key);
void sendMessage(@NonNull final String key, @NonNull final UUID uuid, @NonNull final Consumer<@NonNull ByteArrayDataOutput> consumer);
}
@@ -0,0 +1,101 @@
package net.draycia.carbon.api.users;
import net.draycia.carbon.api.channels.ChatChannel;
import net.kyori.adventure.audience.Audience;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.UUID;
public interface ChatUser extends Audience {
@NonNull
UUID uuid();
@Nullable
String nickname();
default void nickname(@NonNull final String nickname) {
this.nickname(nickname, false);
}
void nickname(@Nullable String nickname, boolean fromRemote);
@Nullable
ChatChannel selectedChannel();
default void selectedChannel(@NonNull final ChatChannel channel) {
this.selectedChannel(channel, false);
}
void selectedChannel(@NonNull ChatChannel channel, boolean fromRemote);
void clearSelectedChannel();
@NonNull
UserChannelSettings channelSettings(@NonNull ChatChannel channel);
boolean spyingwhispers();
default void spyingWhispers(final boolean spyingWhispers) {
this.spyingWhispers(spyingWhispers, false);
}
void spyingWhispers(boolean spyingWhispers, boolean fromRemote);
boolean muted();
default void muted(final boolean muted) {
this.muted(muted, false);
}
void muted(boolean muted, boolean fromRemote);
boolean shadowMuted();
default void shadowMuted(final boolean shadowMuted) {
this.shadowMuted(shadowMuted, false);
}
void shadowMuted(boolean shadowMuted, boolean fromRemote);
@Nullable
UUID replyTarget();
default void replyTarget(@Nullable final UUID target) {
this.replyTarget(target, false);
}
default void replyTarget(@Nullable final ChatUser user) {
this.replyTarget(user.uuid(), false);
}
void replyTarget(@Nullable UUID target, boolean fromRemote);
default void replyTarget(@Nullable final ChatUser user, final boolean fromRemote) {
this.replyTarget(user.uuid(), fromRemote);
}
boolean ignoringUser(@NonNull UUID uuid);
void ignoringUser(@NonNull UUID uuid, boolean ignoring, boolean fromRemote);
default void ignoringUser(@NonNull final UUID uuid, final boolean ignoring) {
this.ignoringUser(uuid, ignoring, false);
}
default boolean ignoringUser(@NonNull final ChatUser user) {
return this.ignoringUser(user.uuid());
}
default void ignoringUser(@NonNull final ChatUser user, final boolean ignoring, final boolean fromRemote) {
this.ignoringUser(user.uuid(), ignoring, fromRemote);
}
default void ignoringUser(@NonNull final ChatUser user, final boolean ignoring) {
this.ignoringUser(user.uuid(), ignoring, false);
}
void sendMessage(@NonNull final ChatUser sender, @NonNull String message);
}
@@ -0,0 +1,33 @@
package net.draycia.carbon.api.users;
import net.kyori.adventure.text.format.TextColor;
import org.checkerframework.checker.nullness.qual.Nullable;
public interface UserChannelSettings {
boolean spying();
default void spying(final boolean spying) {
this.spying(spying, false);
}
void spying(boolean spying, boolean fromRemote);
boolean ignored();
void ignoring(boolean ignored, boolean fromRemote);
default void ignoring(final boolean ignored) {
this.ignoring(ignored, false);
}
@Nullable
TextColor color();
default void color(@Nullable final TextColor color) {
this.color(color, false);
}
void color(@Nullable TextColor color, boolean fromRemote);
}
@@ -0,0 +1,28 @@
package net.draycia.carbon.api.users;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.UUID;
public interface UserService {
@Nullable
ChatUser wrap(String name);
@Nullable
ChatUser wrap(UUID uuid);
@Nullable
ChatUser wrapIfLoaded(@NonNull UUID uuid);
@Nullable
ChatUser refreshUser(@NonNull UUID uuid);
void onDisable();
void invalidate(@NonNull ChatUser user);
void validate(@NonNull ChatUser user);
}