SteamWar/BauSystem2.0
Archiviert
12
0
Dieses Repository wurde am 2024-08-05 archiviert. Du kannst Dateien ansehen und es klonen, aber nicht pushen oder Issues/Pull-Requests öffnen.
BauSystem2.0/BauSystem_Main/src/de/steamwar/bausystem/utils/ProtocolAPI.java
Lixfel fe317bec1e
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful
Full untested impl
2022-03-29 11:43:37 +02:00

74 Zeilen
2.7 KiB
Java

/*
* 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 <https://www.gnu.org/licenses/>.
*/
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<Class<?>, BiFunction<Player, Object, Object>> outgoingHandler = new HashMap<>();
private static final Map<Class<?>, BiFunction<Player, Object, Object>> incomingHandler = new HashMap<>();
static {
TinyProtocol.instance.setOutFilter((receiver, channel, packet) -> {
BiFunction<Player, Object, Object> handler = outgoingHandler.get(packet.getClass());
if (handler == null)
return packet;
return handler.apply(receiver, packet);
});
TinyProtocol.instance.setInFilter((sender, channel, packet) -> {
BiFunction<Player, Object, Object> handler = incomingHandler.get(packet.getClass());
if (handler == null)
return packet;
return handler.apply(sender, packet);
});
}
public static void setOutgoingHandler
(Class<?> packetClass, BiFunction<Player, Object, Object> handler) {
outgoingHandler.put(packetClass, handler);
}
public static void removeOutgoingHandler(Class<?> packetClass) {
outgoingHandler.remove(packetClass);
}
public static void setIncomingHandler
(Class<?> packetClass, BiFunction<Player, Object, Object> 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));
}
}