mirror of
https://github.com/GeyserMC/Floodgate.git
synced 2025-12-30 12:19:22 +00:00
Added a way to handle packets easily. ProtocolSupport support fixed
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2019-2021 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/Floodgate
|
||||
*/
|
||||
|
||||
package org.geysermc.floodgate.api.packet;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
/**
|
||||
* For advanced users only! You shouldn't play with this unless you know what you're doing.
|
||||
*/
|
||||
public interface PacketHandler {
|
||||
/**
|
||||
* Called when a registered packet has been seen.
|
||||
*
|
||||
* @param ctx the channel handler context of the connection
|
||||
* @param packet the packet instance
|
||||
* @param serverbound if the packet is serverbound
|
||||
* @return the packet it should forward. Can be null or a different packet / instance
|
||||
*/
|
||||
Object handle(ChannelHandlerContext ctx, Object packet, boolean serverbound);
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (c) 2019-2021 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/Floodgate
|
||||
*/
|
||||
|
||||
package org.geysermc.floodgate.api.packet;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import org.geysermc.floodgate.api.util.TriFunction;
|
||||
|
||||
public interface PacketHandlers {
|
||||
/**
|
||||
* Register a specific class for a specific consumer.
|
||||
*
|
||||
* @param handler the packet handler instance
|
||||
* @param packetClass the class to start listening for
|
||||
* @param consumer the consumer to call once the packet has been seen
|
||||
*/
|
||||
void register(
|
||||
PacketHandler handler,
|
||||
Class<?> packetClass,
|
||||
TriFunction<ChannelHandlerContext, Object, Boolean, Object> consumer);
|
||||
|
||||
/**
|
||||
* Register a specific class for the given packet handler's {@link
|
||||
* PacketHandler#handle(ChannelHandlerContext, Object, boolean)}.
|
||||
*
|
||||
* @param handler the packet handler instance
|
||||
* @param packetClass the class to start listening for
|
||||
*/
|
||||
default void register(PacketHandler handler, Class<?> packetClass) {
|
||||
register(handler, packetClass, handler::handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register every packet for the given packet handler's {@link PacketHandler#handle(ChannelHandlerContext,
|
||||
* Object, boolean)}
|
||||
*/
|
||||
void registerAll(PacketHandler handler);
|
||||
|
||||
/**
|
||||
* Unregisters all handlers registered under the given packet handler
|
||||
*
|
||||
* @param handler the packet handler instance
|
||||
*/
|
||||
void deregister(PacketHandler handler);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 2019-2021 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/Floodgate
|
||||
*/
|
||||
|
||||
package org.geysermc.floodgate.api.util;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Represents a function that accepts three arguments and produces a result. This is the three-arity
|
||||
* specialization of {@link Function}.
|
||||
*
|
||||
* <p>This is a <a href="package-summary.html">functional interface</a>
|
||||
* whose functional method is {@link #apply(Object, Object, Object)}.
|
||||
*
|
||||
* @param <T> the type of the first argument to the function
|
||||
* @param <U> the type of the second argument to the function
|
||||
* @param <V> the type of the third argument of the function
|
||||
* @param <R> the type of the result of the function
|
||||
* @see Function
|
||||
* @since 1.8
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface TriFunction<T, U, V, R> {
|
||||
/**
|
||||
* Performs this operation on the given arguments.
|
||||
*
|
||||
* @param t the first input argument
|
||||
* @param u the second input argument
|
||||
* @param v the third input argument
|
||||
*/
|
||||
R apply(T t, U u, V v);
|
||||
|
||||
/**
|
||||
* Returns a composed function that first applies this function to its input, and then applies
|
||||
* the {@code after} function to the result. If evaluation of either function throws an
|
||||
* exception, it is relayed to the caller of the composed function.
|
||||
*
|
||||
* @param <S> the type of output of the {@code after} function, and of the composed function
|
||||
* @param after the function to apply after this function is applied
|
||||
* @return a composed function that first applies this function and then applies the {@code
|
||||
* after} function
|
||||
* @throws NullPointerException if after is null
|
||||
*/
|
||||
default <S> TriFunction<T, U, V, S> andThen(Function<? super R, ? extends S> after) {
|
||||
Objects.requireNonNull(after);
|
||||
return (T t, U u, V v) -> after.apply(apply(t, u, v));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user