Replace event library, remove deprecated methods

And some cleanup
This commit is contained in:
Draycia committed 2023-06-06 21:04:41 -07:00
1 parent e87cff5330
commit 5668ad21a8
57 files changed
+278 -488

No files matched your search

+1 -1
View File
@@ -18,7 +18,7 @@ dependencies {
compileOnlyApi(libs.checkerQual)
api(libs.kyoriEventApi)
api(libs.event)
api(libs.kyoriRegistry) {
exclude("com.google.guava")
}
@@ -22,7 +22,7 @@ package net.draycia.carbon.api;
import java.nio.file.Path;
import java.util.UUID;
import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.events.CarbonEventHandler;
import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.users.UserManager;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
@@ -21,7 +21,6 @@ package net.draycia.carbon.api;
import java.util.List;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.UserManager;
import net.kyori.adventure.audience.Audience;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -50,14 +49,4 @@ public interface CarbonServer extends Audience {
*/
List<? extends CarbonPlayer> players();
/**
* Manager used to load/obtain and save {@link CarbonPlayer CarbonPlayers}.
*
* @deprecated Use {@link CarbonChat#userManager} (internal code: inject the UserManager)
* @return the user manager
* @since 2.1.0
*/
@Deprecated(forRemoval = true)
UserManager<? extends CarbonPlayer> userManager();
}
@@ -17,7 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.api.events;
package net.draycia.carbon.api.event;
/**
* Interface to represent that an event belongs to carbon.
@@ -0,0 +1,52 @@
package net.draycia.carbon.api.event;
import com.seiama.event.EventSubscriber;
import com.seiama.event.EventSubscription;
import java.util.function.Consumer;
public interface CarbonEventHandler {
/**
* Registers a subscriber for the given event class.
*
* @param eventClass the class to listen for
* @param subscriber the subscriber that's executed when the event is emitted
* @param <T> the class to listen for
* @return the subscription, so that it may be unregistered
* @since 2.0.0
*/
<T extends CarbonEvent> EventSubscription<T> subscribe(
final Class<T> eventClass,
final EventSubscriber<T> subscriber
);
/**
* Registers a subscriber for the given event class.<br>
* Includes extra values to control when the consumer is executed.
*
* @param eventClass the class to listen for
* @param priority the priority of the consumer
* @param acceptsCancelled if the consumer should be executed if the event is cancelled early
* @param consumer the consumer that's executed when the event is emitted
* @param <T> the class to listen for
* @return the subscription, so that it may be unregistered
* @since 2.0.0
*/
<T extends CarbonEvent> EventSubscription<T> subscribe(
final Class<T> eventClass,
final int priority,
final boolean acceptsCancelled,
final Consumer<T> consumer
);
/**
* Emits the supplied event.<br>
* Events are modified in place, so care should be taken to keep a reference to
* the event while it's being emitted.
*
* @param event the event to be emitted
* @since 2.0.0
*/
void emit(final CarbonEvent event);
}
@@ -17,9 +17,10 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.api.events;
package net.draycia.carbon.api.event.events;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.event.CarbonEvent;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -17,10 +17,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.api.events;
package net.draycia.carbon.api.event.events;
import com.seiama.event.Cancellable;
import java.util.List;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.event.CarbonEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.KeyedRenderer;
import net.kyori.adventure.audience.Audience;
@@ -31,15 +33,13 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import static net.kyori.adventure.text.Component.empty;
/**
* {@link ResultedCarbonEvent} that's called when chat components are rendered for online players.
* Event that's called when chat components are rendered for online players.
*
* @since 2.0.0
*/
@DefaultQualifier(NonNull.class)
public class CarbonChatEvent implements ResultedCarbonEvent<CarbonChatEvent.Result> {
public class CarbonChatEvent implements CarbonEvent, Cancellable {
private final List<KeyedRenderer> renderers;
private final CarbonPlayer sender;
@@ -48,10 +48,10 @@ public class CarbonChatEvent implements ResultedCarbonEvent<CarbonChatEvent.Resu
private final @MonotonicNonNull ChatChannel chatChannel;
private final @MonotonicNonNull SignedMessage signedMessage;
private Component message;
private Result result = Result.ALLOWED;
private boolean cancelled = false;
/**
* {@link ResultedCarbonEvent} that's called when players send messages in chat.
* Event that's called when players send messages in chat.
*
* @param sender the sender of the message
* @param originalMessage the original message that was sent
@@ -161,45 +161,13 @@ public class CarbonChatEvent implements ResultedCarbonEvent<CarbonChatEvent.Resu
}
@Override
public Result result() {
return this.result;
public boolean cancelled() {
return this.cancelled;
}
@Override
public void result(final Result result) {
this.result = result;
}
/**
* The result of this event.
*
* @since 2.0.0
*/
public record Result(boolean cancelled, Component reason) implements ResultedCarbonEvent.Result {
private static final Result ALLOWED = new Result(false, empty());
/**
* Returns a Result that denotes the event was allowed and not cancelled.
*
* @return an allowed result
* @since 2.0.0
*/
public static Result allowed() {
return ALLOWED;
}
/**
* Returns a Result that denotes the event was denied and cancelled.
*
* @param reason the reason the event was denied
* @return a denied result
* @since 2.0.0
*/
public static Result denied(final Component reason) {
return new Result(true, reason);
}
public void cancelled(final boolean cancelled) {
this.cancelled = cancelled;
}
}
@@ -17,10 +17,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.api.events;
package net.draycia.carbon.api.event.events;
// TODO: emit event on server shutdown
import net.draycia.carbon.api.event.CarbonEvent;
/**
* An event that's called when the server shuts down.
*
@@ -1,96 +0,0 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.api.events;
import java.util.function.Consumer;
import net.kyori.event.EventBus;
import net.kyori.event.EventSubscriber;
import net.kyori.event.EventSubscription;
import net.kyori.event.PostResult;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
/**
* Event handler for listening to and emitting carbon events.
*
* @since 1.0.0
*/
@DefaultQualifier(NonNull.class)
public final class CarbonEventHandler {
private final EventBus<CarbonEvent> eventBus = EventBus.create(CarbonEvent.class, (type, event, subscriber) -> {
if (event instanceof ResultedCarbonEvent<@NonNull ?> rce) {
return !rce.result().cancelled();
}
return true;
});
/**
* Registers a subscriber for the given event class.
*
* @param eventClass the class to listen for
* @param subscriber the subscriber that's executed when the event is emitted
* @param <T> the class to listen for
* @return the subscription, so that it may be unregistered
* @since 2.0.0
*/
public <T extends CarbonEvent> EventSubscription subscribe(
final Class<T> eventClass,
final EventSubscriber<T> subscriber
) {
return this.eventBus.subscribe(eventClass, subscriber);
}
/**
* Registers a subscriber for the given event class.<br>
* Includes extra values to control when the consumer is executed.
*
* @param eventClass the class to listen for
* @param priority the priority of the consumer
* @param acceptsCancelled if the consumer should be executed if the event is cancelled early
* @param consumer the consumer that's executed when the event is emitted
* @param <T> the class to listen for
* @return the subscription, so that it may be unregistered
* @since 2.0.0
*/
public <T extends CarbonEvent> EventSubscription subscribe(
final Class<T> eventClass,
final int priority,
final boolean acceptsCancelled,
final Consumer<T> consumer
) {
return this.eventBus.subscribe(eventClass, new EventSubscriberImpl<>(consumer, priority, acceptsCancelled));
}
/**
* Emits the supplied event and returns its result.<br>
* Events are modified in place, so care should be taken to keep a reference to
* the event while it's being emitted.
*
* @param event the event to be emitted
* @return the result
* @since 2.0.0
*/
public PostResult emit(final CarbonEvent event) {
return this.eventBus.post(event);
}
}
@@ -1,33 +0,0 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.api.events;
import java.util.function.Consumer;
import net.kyori.event.EventSubscriber;
import org.checkerframework.checker.nullness.qual.NonNull;
record EventSubscriberImpl<T extends CarbonEvent>(Consumer<T> consumer, int postOrder, boolean acceptsCancelled) implements EventSubscriber<T> {
@Override
public void on(final @NonNull T event) {
this.consumer.accept(event);
}
}
@@ -1,76 +0,0 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.api.events;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
/**
* Represents a {@link CarbonEvent} that has a result.
*
* @since 2.0.0
*/
@DefaultQualifier(NonNull.class)
public interface ResultedCarbonEvent<R extends ResultedCarbonEvent.Result> extends CarbonEvent {
/**
* The result of the event.
*
* @return the event's result
* @since 2.0.0
*/
R result();
/**
* Sets the result of the event.
*
* @param result the result
* @since 2.0.0
*/
void result(final R result);
/**
* Represents the result of the event.
*
* @since 2.0.0
*/
interface Result {
/**
* If the event is cancelled to continue execution.<br>
*
* @return if listeners will continue executing
* @since 2.0.0
*/
boolean cancelled();
/**
* The reason of the result.<br>
* Typically empty unless allowed is false.
*
* @return the reason of the result
* @since 2.0.0
*/
Component reason();
}
}
@@ -1,37 +0,0 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.api.users;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.jetbrains.annotations.ApiStatus;
/**
* The result of a player data operation.
*
* @param player the {@link CarbonPlayer}, or null if unsuccessful
* @param reason the reason of the result, typically empty unless {@link #player} is null
* @since 2.0.0
*/
@Deprecated(forRemoval = true)
@ApiStatus.Internal
public record ComponentPlayerResult<C extends CarbonPlayer>(@MonotonicNonNull C player, Component reason) {
}
@@ -21,8 +21,6 @@ package net.draycia.carbon.api.users;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.util.ComponentMessageThrowable;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -34,21 +32,6 @@ import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public interface UserManager<C extends CarbonPlayer> {
/**
* Loads and returns a {@link CarbonPlayer} with the given {@link UUID}.
*
* @deprecated use {@link #user(UUID)} instead
* @param uuid the player's uuid
* @return the result
* @since 2.0.0
*/
@Deprecated(forRemoval = true)
default CompletableFuture<ComponentPlayerResult<C>> carbonPlayer(final UUID uuid) {
return this.user(uuid)
.thenApply(user -> new ComponentPlayerResult<>(user, Component.empty()))
.exceptionally(thr -> new ComponentPlayerResult<>(null, ComponentMessageThrowable.getOrConvertMessage(thr)));
}
/**
* Gets the {@link CarbonPlayer} for the provided player {@link UUID}, whether they are online or not.
*