/* * This file is a part of the SteamWar software. * * Copyright (C) 2021 SteamWar.de-Serverteam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package de.steamwar.bausystem.utils; import com.comphenix.tinyprotocol.TinyProtocol; import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import java.util.HashMap; import java.util.Map; import java.util.function.BiFunction; @UtilityClass public class ProtocolAPI { private static final Map, BiFunction> outgoingHandler = new HashMap<>(); private static final Map, BiFunction> incomingHandler = new HashMap<>(); static { TinyProtocol.instance.setOutFilter((receiver, channel, packet) -> { BiFunction handler = outgoingHandler.get(packet.getClass()); if (handler == null) return packet; return handler.apply(receiver, packet); }); TinyProtocol.instance.setInFilter((sender, channel, packet) -> { BiFunction handler = incomingHandler.get(packet.getClass()); if (handler == null) return packet; return handler.apply(sender, packet); }); } public static void setOutgoingHandler (Class packetClass, BiFunction handler) { outgoingHandler.put(packetClass, handler); } public static void removeOutgoingHandler(Class packetClass) { outgoingHandler.remove(packetClass); } public static void setIncomingHandler (Class packetClass, BiFunction handler) { incomingHandler.put(packetClass, handler); } public static void removeIncomingHandler(Class packetClass) { incomingHandler.remove(packetClass); } public static void broadcastPacket(Object packet) { Bukkit.getOnlinePlayers().stream().map(TinyProtocol.instance::getChannel).filter(TinyProtocol.instance::hasInjected).forEach(channel -> TinyProtocol.instance.sendPacket(channel, packet)); } }