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    this.tag = tag;
040  }
041
042  public void handle(byte[] bytes) {
043
044    try(ChannelBytesWrapper wrapper = new ChannelBytesWrapper(bytes)) {
045
046      Optional<UUID> serverID = Optional.empty();
047      try {
048        serverID = wrapper.readUUID();
049      } catch (IOException e) {
050        e.printStackTrace();
051      }
052
053      if(serverID.isPresent()) {
054        server = serverID.get();
055
056        PluginCore.log().debug("Message Received:", DebugLevel.DEVELOPER);
057        PluginCore.log().debug("Received:" + server, DebugLevel.DEVELOPER);
058        if(!PluginCore.instance().getServerID().equals(server)) {
059          handle(wrapper);
060        }
061      }
062    }
063  }
064
065  public abstract void handle(ChannelBytesWrapper wrapper);
066}