Search in sources :

Example 1 with ClientboundPageChangeResponsePacket

use of com.almuradev.almura.feature.guide.network.ClientboundPageChangeResponsePacket in project Almura by AlmuraDev.

the class ServerboundPageChangeRequestPacketHandler method handleMessage.

@Override
public void handleMessage(ServerboundPageChangeRequestPacket message, RemoteConnection connection, Platform.Type side) {
    if (side.isServer() && connection instanceof PlayerConnection && message.changeType != null && Sponge.isServerAvailable()) {
        final MinecraftServer server = (MinecraftServer) Sponge.getServer();
        if (PacketUtil.checkThreadAndEnqueue(server, message, this, connection, side)) {
            final Player player = ((PlayerConnection) connection).getPlayer();
            // Alert player that ID was missing
            if (message.id.isEmpty()) {
                this.network.sendTo(player, new ClientboundPageChangeResponsePacket(message.changeType, false, message.id, "almura.guide.action.any.missing_id"));
                return;
            }
            // Alert player that name was missing
            if (message.changeType != PageChangeType.REMOVE && message.name.isEmpty()) {
                this.network.sendTo(player, new ClientboundPageChangeResponsePacket(message.changeType, false, message.id, "almura.guide.action.any.missing_name"));
                return;
            }
            Page page = this.manager.getPage(message.id).orElse(null);
            if (message.changeType == PageChangeType.ADD) {
                // If the id being sent up is already in the manager, we've got a desync
                if (page != null) {
                    this.network.sendTo(player, new ClientboundPageListingsPacket(this.manager.getAvailablePagesFor(player).entrySet().stream().map(entry -> new PageListEntry(entry.getKey(), entry.getValue().getName())).collect(Collectors.toList()), null));
                    this.network.sendTo(player, new ClientboundPageChangeResponsePacket(message.changeType, false, message.id, "almura.guide.action.add.id_exists"));
                    return;
                } else {
                    if (!player.hasPermission("almura.guide.add")) {
                        this.network.sendTo(player, new ClientboundPageChangeResponsePacket(message.changeType, false, message.id, "almura.guide.permission.add.missing"));
                        return;
                    }
                    page = new Page(message.id, player.getUniqueId());
                    this.manager.addPage(page);
                }
            } else if (message.changeType == PageChangeType.MODIFY || message.changeType == PageChangeType.REMOVE) {
                // Sent up a modify or remove of a page but someone deleted it, we've got a desync
                if (page == null) {
                    this.network.sendTo(player, new ClientboundPageListingsPacket(this.manager.getAvailablePagesFor(player).entrySet().stream().map(entry -> new PageListEntry(entry.getKey(), entry.getValue().getName())).collect(Collectors.toList()), null));
                    return;
                }
                if (message.changeType == PageChangeType.MODIFY) {
                    if (!player.hasPermission("almura.guide.modify." + message.id)) {
                        this.network.sendTo(player, new ClientboundPageChangeResponsePacket(message.changeType, false, message.id, "almura.guide.permission.modify.missing"));
                        return;
                    }
                } else if (message.changeType == PageChangeType.REMOVE) {
                    if (!player.hasPermission("almura.guide.remove." + message.id)) {
                        this.network.sendTo(player, new ClientboundPageChangeResponsePacket(message.changeType, false, message.id, "almura.guide.permission.remove.missing"));
                        return;
                    }
                    this.manager.deletePage(message.id);
                }
            }
            if (message.changeType != PageChangeType.REMOVE && page != null) {
                page.setLastModifier(player.getUniqueId());
                page.setLastModified(Instant.now());
                page.setIndex(message.index);
                page.setName(message.name);
                page.setContent(message.content);
                if (message.changeType == PageChangeType.ADD) {
                    this.manager.savePage(page, false);
                } else {
                    this.manager.savePage(page, true);
                }
            }
            // Let the player know they were successful
            this.network.sendTo(player, new ClientboundPageChangeResponsePacket(message.changeType, true, message.id, "almura.guide.action." + message.changeType.name().toLowerCase() + ".success"));
            // Sync the listings to the player who caused this change and put them on that page (only to fix switching on creation).
            this.network.sendTo(player, new ClientboundPageListingsPacket(this.manager.getAvailablePagesFor(player).entrySet().stream().map(entry -> new PageListEntry(entry.getKey(), entry.getValue().getName())).collect(Collectors.toList()), message.id));
            // Sync the listings to everyone else
            this.game.getServer().getOnlinePlayers().stream().filter(p -> !p.getUniqueId().equals(player.getUniqueId())).forEach((online) -> this.network.sendTo(online, new ClientboundPageListingsPacket(this.manager.getAvailablePagesFor(player).entrySet().stream().map(entry -> new PageListEntry(entry.getKey(), entry.getValue().getName())).collect(Collectors.toList()), null)));
        }
    }
}
Also used : Page(com.almuradev.almura.feature.guide.Page) ClientboundPageChangeResponsePacket(com.almuradev.almura.feature.guide.network.ClientboundPageChangeResponsePacket) RemoteConnection(org.spongepowered.api.network.RemoteConnection) ServerboundPageChangeRequestPacket(com.almuradev.almura.feature.guide.network.ServerboundPageChangeRequestPacket) Game(org.spongepowered.api.Game) ClientboundPageListingsPacket(com.almuradev.almura.feature.guide.network.ClientboundPageListingsPacket) ServerPageManager(com.almuradev.almura.feature.guide.ServerPageManager) Sponge(org.spongepowered.api.Sponge) NetworkConfig(com.almuradev.almura.shared.network.NetworkConfig) MessageHandler(org.spongepowered.api.network.MessageHandler) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) PlayerConnection(org.spongepowered.api.network.PlayerConnection) PageListEntry(com.almuradev.almura.feature.guide.PageListEntry) PacketUtil(com.almuradev.almura.shared.util.PacketUtil) Platform(org.spongepowered.api.Platform) ChannelBinding(org.spongepowered.api.network.ChannelBinding) Inject(javax.inject.Inject) MinecraftServer(net.minecraft.server.MinecraftServer) ChannelId(org.spongepowered.api.network.ChannelId) Player(org.spongepowered.api.entity.living.player.Player) PluginContainer(org.spongepowered.api.plugin.PluginContainer) PageChangeType(com.almuradev.almura.feature.guide.network.PageChangeType) Player(org.spongepowered.api.entity.living.player.Player) ClientboundPageChangeResponsePacket(com.almuradev.almura.feature.guide.network.ClientboundPageChangeResponsePacket) Page(com.almuradev.almura.feature.guide.Page) ClientboundPageListingsPacket(com.almuradev.almura.feature.guide.network.ClientboundPageListingsPacket) PlayerConnection(org.spongepowered.api.network.PlayerConnection) MinecraftServer(net.minecraft.server.MinecraftServer) PageListEntry(com.almuradev.almura.feature.guide.PageListEntry)

Aggregations

Page (com.almuradev.almura.feature.guide.Page)1 PageListEntry (com.almuradev.almura.feature.guide.PageListEntry)1 ServerPageManager (com.almuradev.almura.feature.guide.ServerPageManager)1 ClientboundPageChangeResponsePacket (com.almuradev.almura.feature.guide.network.ClientboundPageChangeResponsePacket)1 ClientboundPageListingsPacket (com.almuradev.almura.feature.guide.network.ClientboundPageListingsPacket)1 PageChangeType (com.almuradev.almura.feature.guide.network.PageChangeType)1 ServerboundPageChangeRequestPacket (com.almuradev.almura.feature.guide.network.ServerboundPageChangeRequestPacket)1 NetworkConfig (com.almuradev.almura.shared.network.NetworkConfig)1 PacketUtil (com.almuradev.almura.shared.util.PacketUtil)1 Instant (java.time.Instant)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 MinecraftServer (net.minecraft.server.MinecraftServer)1 Game (org.spongepowered.api.Game)1 Platform (org.spongepowered.api.Platform)1 Sponge (org.spongepowered.api.Sponge)1 Player (org.spongepowered.api.entity.living.player.Player)1 ChannelBinding (org.spongepowered.api.network.ChannelBinding)1 ChannelId (org.spongepowered.api.network.ChannelId)1 MessageHandler (org.spongepowered.api.network.MessageHandler)1