// Path: bungee/src/main/java/codecrafter47/bungeetablistplus/protocol/AbstractPacketHandler.java
// public abstract class AbstractPacketHandler implements PacketHandler {
// 
//     private final PacketHandler parent;
// 
//     public AbstractPacketHandler(@Nonnull @NonNull PacketHandler parent) {
//         this.parent = parent;
//     }
// 
//     @Override
//     public PacketListenerResult onPlayerListPacket(PlayerListItem packet) {
//         return parent.onPlayerListPacket(packet);
//     }
// 
//     @Override
//     public PacketListenerResult onTeamPacket(Team packet) {
//         return parent.onTeamPacket(packet);
//     }
// 
//     @Override
//     public PacketListenerResult onPlayerListHeaderFooterPacket(PlayerListHeaderFooter packet) {
//         return parent.onPlayerListHeaderFooterPacket(packet);
//     }
// 
//     @Override
//     public void onServerSwitch(boolean is13OrLater) {
//         parent.onServerSwitch(is13OrLater);
//     }
// }
// 
// Path: bungee/src/main/java/codecrafter47/bungeetablistplus/protocol/PacketHandler.java
// public interface PacketHandler {
// 
//     PacketListenerResult onPlayerListPacket(PlayerListItem packet);
// 
//     PacketListenerResult onTeamPacket(Team packet);
// 
//     PacketListenerResult onPlayerListHeaderFooterPacket(PlayerListHeaderFooter packet);
// 
//     void onServerSwitch(boolean is13OrLater);
// }
// 
// Path: bungee/src/main/java/codecrafter47/bungeetablistplus/protocol/PacketListenerResult.java
// public enum PacketListenerResult {
//     PASS, MODIFIED, CANCEL;
// }


import codecrafter47.bungeetablistplus.protocol.AbstractPacketHandler;
import codecrafter47.bungeetablistplus.protocol.PacketHandler;
import codecrafter47.bungeetablistplus.protocol.PacketListenerResult;
import com.google.common.base.MoreObjects;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.connection.LoginResult;
import net.md_5.bungee.protocol.packet.PlayerListItem;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/*
 *     Copyright (C) 2020 Florian Stober
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU 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 General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package codecrafter47.bungeetablistplus.handler;



public class RewriteLogic extends AbstractPacketHandler {

    private final Map<UUID, UUID> rewriteMap = new HashMap<>();

    public RewriteLogic(PacketHandler parent) {
        super(parent);
    }

    @Override
    public PacketListenerResult onPlayerListPacket(PlayerListItem packet) {

        if (packet.getAction() == PlayerListItem.Action.ADD_PLAYER) {
            for (PlayerListItem.Item item : packet.getItems()) {
                UUID uuid = item.getUuid();
                UserConnection player = BungeeCord.getInstance().getPlayerByOfflineUUID(uuid);
                if (player != null) {
                    rewriteMap.put(uuid, player.getUniqueId());
                }
            }
        }

        boolean modified = false;

        if (packet.getAction() == PlayerListItem.Action.REMOVE_PLAYER) {
            for (PlayerListItem.Item item : packet.getItems()) {
                UUID uuid = rewriteMap.remove(item.getUuid());
                modified |= uuid != null;
                item.setUuid(MoreObjects.firstNonNull(uuid, item.getUuid()));
            }
        } else {
            for (PlayerListItem.Item item : packet.getItems()) {
                UUID uuid = rewriteMap.get(item.getUuid());
                if (uuid != null) {
// Path: bungee/src/main/java/codecrafter47/bungeetablistplus/protocol/AbstractPacketHandler.java
// public abstract class AbstractPacketHandler implements PacketHandler {
// 
//     private final PacketHandler parent;
// 
//     public AbstractPacketHandler(@Nonnull @NonNull PacketHandler parent) {
//         this.parent = parent;
//     }
// 
//     @Override
//     public PacketListenerResult onPlayerListPacket(PlayerListItem packet) {
//         return parent.onPlayerListPacket(packet);
//     }
// 
//     @Override
//     public PacketListenerResult onTeamPacket(Team packet) {
//         return parent.onTeamPacket(packet);
//     }
// 
//     @Override
//     public PacketListenerResult onPlayerListHeaderFooterPacket(PlayerListHeaderFooter packet) {
//         return parent.onPlayerListHeaderFooterPacket(packet);
//     }
// 
//     @Override
//     public void onServerSwitch(boolean is13OrLater) {
//         parent.onServerSwitch(is13OrLater);
//     }
// }
// 
// Path: bungee/src/main/java/codecrafter47/bungeetablistplus/protocol/PacketHandler.java
// public interface PacketHandler {
// 
//     PacketListenerResult onPlayerListPacket(PlayerListItem packet);
// 
//     PacketListenerResult onTeamPacket(Team packet);
// 
//     PacketListenerResult onPlayerListHeaderFooterPacket(PlayerListHeaderFooter packet);
// 
//     void onServerSwitch(boolean is13OrLater);
// }
// 
// Path: bungee/src/main/java/codecrafter47/bungeetablistplus/protocol/PacketListenerResult.java
// public enum PacketListenerResult {
//     PASS, MODIFIED, CANCEL;
// }


// Path: bungee/src/main/java/codecrafter47/bungeetablistplus/handler/RewriteLogic.java
import codecrafter47.bungeetablistplus.protocol.AbstractPacketHandler;
import codecrafter47.bungeetablistplus.protocol.PacketHandler;
import codecrafter47.bungeetablistplus.protocol.PacketListenerResult;
import com.google.common.base.MoreObjects;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.connection.LoginResult;
import net.md_5.bungee.protocol.packet.PlayerListItem;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/*
 *     Copyright (C) 2020 Florian Stober
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU 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 General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package codecrafter47.bungeetablistplus.handler;



public class RewriteLogic extends AbstractPacketHandler {

    private final Map<UUID, UUID> rewriteMap = new HashMap<>();

    public RewriteLogic(PacketHandler parent) {
        super(parent);
    }

    @Override
    public PacketListenerResult onPlayerListPacket(PlayerListItem packet) {

        if (packet.getAction() == PlayerListItem.Action.ADD_PLAYER) {
            for (PlayerListItem.Item item : packet.getItems()) {
                UUID uuid = item.getUuid();
                UserConnection player = BungeeCord.getInstance().getPlayerByOfflineUUID(uuid);
                if (player != null) {
                    rewriteMap.put(uuid, player.getUniqueId());
                }
            }
        }

        boolean modified = false;

        if (packet.getAction() == PlayerListItem.Action.REMOVE_PLAYER) {
            for (PlayerListItem.Item item : packet.getItems()) {
                UUID uuid = rewriteMap.remove(item.getUuid());
                modified |= uuid != null;
                item.setUuid(MoreObjects.firstNonNull(uuid, item.getUuid()));
            }
        } else {
            for (PlayerListItem.Item item : packet.getItems()) {
                UUID uuid = rewriteMap.get(item.getUuid());
                if (uuid != null) {
                    modified = true;