Allow events to be registered by any class

Supersedes & closes #3073

Co-authored-by: Redned <redned235@gmail.com>
This commit is contained in:
ImDaBigBoss 2022-09-04 16:11:08 -05:00 committed by RednedEpic
parent db3b470225
commit f1da9d7072
25 changed files with 353 additions and 115 deletions

View file

@ -39,6 +39,7 @@ public class Geyser {
*
* @return the base api
*/
@NonNull
public static GeyserApiBase api() {
if (api == null) {
throw new RuntimeException("Api has not been registered yet!");

View file

@ -25,6 +25,8 @@
package org.geysermc.api.util;
import org.checkerframework.checker.nullness.qual.NonNull;
public enum BedrockPlatform {
UNKNOWN("Unknown"),
GOOGLE("Android"),
@ -56,6 +58,7 @@ public enum BedrockPlatform {
* @param id the BedrockPlatform identifier
* @return The BedrockPlatform or {@link #UNKNOWN} if the platform wasn't found
*/
@NonNull
public static BedrockPlatform fromId(int id) {
return id < VALUES.length ? VALUES[id] : VALUES[0];
}

View file

@ -25,6 +25,8 @@
package org.geysermc.api.util;
import org.checkerframework.checker.nullness.qual.NonNull;
public enum InputMode {
UNKNOWN,
KEYBOARD_MOUSE,
@ -40,6 +42,7 @@ public enum InputMode {
* @param id the InputMode identifier
* @return The InputMode or {@link #UNKNOWN} if the mode wasn't found
*/
@NonNull
public static InputMode fromId(int id) {
return VALUES.length > id ? VALUES[id] : VALUES[0];
}

View file

@ -25,6 +25,8 @@
package org.geysermc.api.util;
import org.checkerframework.checker.nullness.qual.NonNull;
public enum UiProfile {
CLASSIC, POCKET;
@ -36,6 +38,7 @@ public enum UiProfile {
* @param id the UiProfile identifier
* @return The UiProfile or {@link #CLASSIC} if the profile wasn't found
*/
@NonNull
public static UiProfile fromId(int id) {
return VALUES.length > id ? VALUES[id] : VALUES[0];
}

View file

@ -31,6 +31,7 @@ import org.geysermc.api.Geyser;
import org.geysermc.api.GeyserApiBase;
import org.geysermc.geyser.api.connection.GeyserConnection;
import org.geysermc.geyser.api.event.EventBus;
import org.geysermc.geyser.api.event.EventRegistrar;
import org.geysermc.geyser.api.extension.ExtensionManager;
import org.geysermc.geyser.api.network.BedrockListener;
import org.geysermc.geyser.api.network.RemoteServer;
@ -86,7 +87,7 @@ public interface GeyserApi extends GeyserApiBase {
* @return the event bus
*/
@NonNull
EventBus eventBus();
EventBus<EventRegistrar> eventBus();
/**
* Gets the default {@link RemoteServer} configured

View file

@ -36,8 +36,8 @@ import java.util.Set;
* Represents a bus capable of subscribing
* or "listening" to events and firing them.
*/
public interface EventBus extends OwnedEventBus<Extension, Event, EventSubscriber<? extends Event>> {
public interface EventBus<R extends EventRegistrar> extends OwnedEventBus<R, Event, EventSubscriber<R, ? extends Event>> {
@Override
@NonNull
<T extends Event> Set<? extends EventSubscriber<T>> subscribers(@NonNull Class<T> eventClass);
<T extends Event> Set<? extends EventSubscriber<R, T>> subscribers(@NonNull Class<T> eventClass);
}

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.event;
import org.geysermc.geyser.api.GeyserApi;
/**
* Represents an owner for an event that allows it
* to be registered through an {@link EventBus}.
*/
public interface EventRegistrar {
/**
* Creates an {@link EventRegistrar} instance.
*
* @param object the object to wrap around
* @return an event registrar instance
*/
static EventRegistrar of(Object object) {
return GeyserApi.api().provider(EventRegistrar.class, object);
}
}

View file

@ -36,5 +36,5 @@ import org.geysermc.geyser.api.extension.Extension;
*
* @param <T> the class of the event
*/
public interface EventSubscriber<T extends Event> extends OwnedSubscriber<Extension, T> {
public interface EventSubscriber<R extends EventRegistrar, T extends Event> extends OwnedSubscriber<R, T> {
}

View file

@ -27,6 +27,7 @@ package org.geysermc.geyser.api.event;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.event.Event;
import org.geysermc.geyser.api.extension.Extension;
import java.util.Set;
@ -34,7 +35,7 @@ import java.util.Set;
* An {@link EventBus} with additional methods that implicitly
* set the extension instance.
*/
public interface ExtensionEventBus extends org.geysermc.event.bus.EventBus<Event, EventSubscriber<? extends Event>> {
public interface ExtensionEventBus extends org.geysermc.event.bus.EventBus<Event, EventSubscriber<Extension, ? extends Event>> {
@Override
@NonNull <T extends Event> Set<? extends EventSubscriber<T>> subscribers(@NonNull Class<T> eventClass);
@NonNull <T extends Event> Set<? extends EventSubscriber<EventRegistrar, T>> subscribers(@NonNull Class<T> eventClass);
}

View file

@ -45,6 +45,7 @@ public interface GeyserDefineCustomItemsEvent extends Event {
*
* @return a multimap of all the already registered custom items
*/
@NonNull
Map<String, Collection<CustomItemData>> getExistingCustomItems();
/**
@ -52,6 +53,7 @@ public interface GeyserDefineCustomItemsEvent extends Event {
*
* @return the list of the already registered non-vanilla custom items
*/
@NonNull
List<NonVanillaCustomItemData> getExistingNonVanillaCustomItems();
/**

View file

@ -28,6 +28,7 @@ package org.geysermc.geyser.api.event.lifecycle;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.event.Event;
import org.geysermc.geyser.api.event.EventBus;
import org.geysermc.geyser.api.event.EventRegistrar;
import org.geysermc.geyser.api.extension.ExtensionManager;
/**
@ -36,5 +37,5 @@ import org.geysermc.geyser.api.extension.ExtensionManager;
* @param extensionManager the extension manager
* @param eventBus the event bus
*/
public record GeyserPostInitializeEvent(@NonNull ExtensionManager extensionManager, @NonNull EventBus eventBus) implements Event {
public record GeyserPostInitializeEvent(@NonNull ExtensionManager extensionManager, @NonNull EventBus<EventRegistrar> eventBus) implements Event {
}

View file

@ -28,6 +28,7 @@ package org.geysermc.geyser.api.event.lifecycle;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.event.Event;
import org.geysermc.geyser.api.event.EventBus;
import org.geysermc.geyser.api.event.EventRegistrar;
import org.geysermc.geyser.api.extension.ExtensionManager;
/**
@ -36,5 +37,5 @@ import org.geysermc.geyser.api.extension.ExtensionManager;
* @param extensionManager the extension manager
* @param eventBus the event bus
*/
public record GeyserPreInitializeEvent(@NonNull ExtensionManager extensionManager, @NonNull EventBus eventBus) implements Event {
public record GeyserPreInitializeEvent(@NonNull ExtensionManager extensionManager, @NonNull EventBus<EventRegistrar> eventBus) implements Event {
}

View file

@ -28,10 +28,11 @@ package org.geysermc.geyser.api.event.lifecycle;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.event.Event;
import org.geysermc.geyser.api.event.EventBus;
import org.geysermc.geyser.api.event.EventRegistrar;
import org.geysermc.geyser.api.extension.ExtensionManager;
/**
* Called when Geyser is shutting down.
*/
public record GeyserShutdownEvent(@NonNull ExtensionManager extensionManager, @NonNull EventBus eventBus) implements Event {
public record GeyserShutdownEvent(@NonNull ExtensionManager extensionManager, @NonNull EventBus<EventRegistrar> eventBus) implements Event {
}

View file

@ -25,16 +25,19 @@
package org.geysermc.geyser.api.extension;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.api.GeyserApiBase;
import org.geysermc.geyser.api.GeyserApi;
import org.geysermc.geyser.api.event.EventRegistrar;
import org.geysermc.geyser.api.event.ExtensionEventBus;
import java.nio.file.Path;
import java.util.Objects;
/**
* Represents an extension within Geyser.
*/
public interface Extension {
public interface Extension extends EventRegistrar {
/**
* Gets if the extension is enabled
@ -59,6 +62,7 @@ public interface Extension {
*
* @return the extension's data folder
*/
@NonNull
default Path dataFolder() {
return this.extensionLoader().dataFolder(this);
}
@ -68,6 +72,7 @@ public interface Extension {
*
* @return the extension event bus
*/
@NonNull
default ExtensionEventBus eventBus() {
return this.extensionLoader().eventBus(this);
}
@ -77,6 +82,7 @@ public interface Extension {
*
* @return the extension manager
*/
@NonNull
default ExtensionManager extensionManager() {
return this.geyserApi().extensionManager();
}
@ -86,6 +92,7 @@ public interface Extension {
*
* @return the extension's name
*/
@NonNull
default String name() {
return this.description().name();
}
@ -95,6 +102,7 @@ public interface Extension {
*
* @return the extension's description
*/
@NonNull
default ExtensionDescription description() {
return this.extensionLoader().description(this);
}
@ -104,6 +112,7 @@ public interface Extension {
*
* @return the extension's logger
*/
@NonNull
default ExtensionLogger logger() {
return this.extensionLoader().logger(this);
}
@ -113,8 +122,9 @@ public interface Extension {
*
* @return the extension loader
*/
@NonNull
default ExtensionLoader extensionLoader() {
return this.extensionManager().extensionLoader(this);
return Objects.requireNonNull(this.extensionManager().extensionLoader(this));
}
/**
@ -122,6 +132,7 @@ public interface Extension {
*
* @return the geyser api instance
*/
@NonNull
default GeyserApi geyserApi() {
return GeyserApi.api();
}