2019-07-17 18:00:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 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
|
|
|
|
*/
|
|
|
|
|
2019-07-11 22:39:28 +00:00
|
|
|
package org.geysermc.connector.network.translators;
|
|
|
|
|
|
|
|
import com.github.steveice10.packetlib.packet.Packet;
|
2019-07-24 20:12:42 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.BedrockPacket;
|
2019-07-19 00:11:58 +00:00
|
|
|
import org.geysermc.connector.console.GeyserLogger;
|
2019-07-13 19:00:51 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2019-07-11 22:39:28 +00:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public class Registry<T> {
|
2019-07-24 20:12:42 +00:00
|
|
|
private final Map<Class<? extends T>, PacketTranslator<? extends T>> MAP = new HashMap<>();
|
2019-07-11 22:39:28 +00:00
|
|
|
|
|
|
|
public static final Registry<Packet> JAVA = new Registry<>();
|
2019-07-24 20:12:42 +00:00
|
|
|
public static final Registry<BedrockPacket> BEDROCK = new Registry<>();
|
2019-07-11 22:39:28 +00:00
|
|
|
|
2019-07-24 20:12:42 +00:00
|
|
|
public static <T extends Packet> void registerJava(Class<T> clazz, PacketTranslator<T> translator) {
|
2019-07-11 22:39:28 +00:00
|
|
|
JAVA.MAP.put(clazz, translator);
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:12:42 +00:00
|
|
|
public static <T extends BedrockPacket> void registerBedrock(Class<T> clazz, PacketTranslator<T> translator) {
|
|
|
|
BEDROCK.MAP.put(clazz, translator);
|
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
public <P extends T> boolean translate(Class<? extends P> clazz, P packet, GeyserSession session) {
|
2019-10-02 20:45:29 +00:00
|
|
|
if (!session.getUpstream().isClosed() && !session.isClosed()) {
|
|
|
|
try {
|
|
|
|
if (MAP.containsKey(clazz)) {
|
|
|
|
((PacketTranslator<P>) MAP.get(clazz)).translate(packet, session);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
GeyserLogger.DEFAULT.error("Could not translate packet " + packet.getClass().getSimpleName(), ex);
|
|
|
|
ex.printStackTrace();
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
return false;
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
|
|
|
}
|