001package net.tnemc.plugincore.core.channel; 002/* 003 * The New Plugin Core 004 * Copyright (C) 2022 - 2024 Daniel "creatorfromhell" Vidmar 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU Affero General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU Affero General Public License for more details. 015 * 016 * You should have received a copy of the GNU Affero General Public License 017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 018 */ 019 020import net.tnemc.plugincore.PluginCore; 021import net.tnemc.plugincore.core.compatibility.log.DebugLevel; 022 023import java.io.IOException; 024import java.util.Optional; 025import java.util.UUID; 026 027/** 028 * ChannelMessageHandler represents a handler for a plugin channel message. 029 * 030 * @author creatorfromhell 031 * @since 0.1.2.0 032 */ 033public abstract class ChannelMessageHandler { 034 035 protected final String tag; 036 protected UUID server; 037 038 public ChannelMessageHandler(String tag) { 039 040 this.tag = tag; 041 } 042 043 public void handle(byte[] bytes) { 044 045 try(ChannelBytesWrapper wrapper = new ChannelBytesWrapper(bytes)) { 046 047 Optional<UUID> serverID = Optional.empty(); 048 try { 049 serverID = wrapper.readUUID(); 050 } catch(IOException e) { 051 e.printStackTrace(); 052 } 053 054 if(serverID.isPresent()) { 055 server = serverID.get(); 056 057 PluginCore.log().debug("Message Received:", DebugLevel.DEVELOPER); 058 PluginCore.log().debug("Received:" + server, DebugLevel.DEVELOPER); 059 if(!PluginCore.instance().getServerID().equals(server)) { 060 handle(wrapper); 061 } 062 } 063 } 064 } 065 066 public abstract void handle(ChannelBytesWrapper wrapper); 067}