Search in sources :

Example 1 with Page

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

the class ServerboundGuideOpenRequestPacketHandler method handleMessage.

@Override
public void handleMessage(ServerboundGuideOpenRequestPacket message, RemoteConnection connection, Platform.Type side) {
    if (side.isServer() && connection instanceof PlayerConnection && Sponge.isServerAvailable()) {
        final MinecraftServer server = (MinecraftServer) Sponge.getServer();
        if (PacketUtil.checkThreadAndEnqueue(server, message, this, connection, side)) {
            final Player player = ((PlayerConnection) connection).getPlayer();
            if (!player.hasPermission("almura.guide.open") || player.hasPermission("almura.singleplayer") && MalisisCore.isObfEnv) {
                player.sendMessage(Text.of(TextColors.WHITE, "Access denied, missing permission: ", TextColors.AQUA, "almura.guide.open", TextColors.WHITE, "."));
                return;
            }
            // Open the GUI
            this.network.sendTo(player, new ClientboundGuideOpenResponsePacket(GuideOpenType.PLAYER_INVOKED_KEYBIND, player.hasPermission("almura.guide.add"), player.hasPermission("almura.guide.remove"), player.hasPermission("almura.guide.modify"), true));
            final Map<String, Page> pagesToSend = this.manager.getAvailablePagesFor(player);
            if (pagesToSend.size() > 0) {
                final List<PageListEntry> playerListings = pagesToSend.entrySet().stream().map(entry -> new PageListEntry(entry.getKey(), entry.getValue().getName())).collect(Collectors.toList());
                final PageListEntry switchToPageEntry = playerListings.stream().findFirst().orElse(null);
                // Send the list of pages
                this.network.sendTo(player, new ClientboundPageListingsPacket(playerListings, switchToPageEntry == null ? null : switchToPageEntry.getId()));
            }
        }
    }
}
Also used : Page(com.almuradev.almura.feature.guide.Page) RemoteConnection(org.spongepowered.api.network.RemoteConnection) ServerPageManager(com.almuradev.almura.feature.guide.ServerPageManager) Platform(org.spongepowered.api.Platform) Inject(javax.inject.Inject) MinecraftServer(net.minecraft.server.MinecraftServer) ServerboundGuideOpenRequestPacket(com.almuradev.almura.feature.guide.network.ServerboundGuideOpenRequestPacket) Text(org.spongepowered.api.text.Text) Minecraft(net.minecraft.client.Minecraft) Map(java.util.Map) PluginContainer(org.spongepowered.api.plugin.PluginContainer) TextColors(org.spongepowered.api.text.format.TextColors) GuideOpenType(com.almuradev.almura.feature.guide.network.GuideOpenType) ClientboundPageListingsPacket(com.almuradev.almura.feature.guide.network.ClientboundPageListingsPacket) MalisisCore(net.malisis.core.MalisisCore) Scheduler(org.spongepowered.api.scheduler.Scheduler) ClientboundGuideOpenResponsePacket(com.almuradev.almura.feature.guide.network.ClientboundGuideOpenResponsePacket) Sponge(org.spongepowered.api.Sponge) NetworkConfig(com.almuradev.almura.shared.network.NetworkConfig) MessageHandler(org.spongepowered.api.network.MessageHandler) 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) ChannelBinding(org.spongepowered.api.network.ChannelBinding) List(java.util.List) ChannelId(org.spongepowered.api.network.ChannelId) Player(org.spongepowered.api.entity.living.player.Player) Player(org.spongepowered.api.entity.living.player.Player) ClientboundGuideOpenResponsePacket(com.almuradev.almura.feature.guide.network.ClientboundGuideOpenResponsePacket) 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)

Example 2 with Page

use of com.almuradev.almura.feature.guide.Page 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)

Example 3 with Page

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

the class ServerboundPageOpenRequestPacketHandler method handleMessage.

@Override
public void handleMessage(ServerboundPageOpenRequestPacket message, RemoteConnection connection, Platform.Type side) {
    if (side.isServer() && connection instanceof PlayerConnection && Sponge.isServerAvailable()) {
        final MinecraftServer server = (MinecraftServer) Sponge.getServer();
        if (PacketUtil.checkThreadAndEnqueue(server, message, this, connection, side)) {
            final Player player = ((PlayerConnection) connection).getPlayer();
            final Page page = this.manager.getPage(message.id).orElse(null);
            if (!player.hasPermission("almura.guide.page." + message.id) || page == null) {
                // We do not have permission for a page or it is null then we have a de-sync. Re-send all page names
                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));
            } else {
                this.network.sendTo(player, new ClientboundPageOpenResponsePacket(page));
            }
        }
    }
}
Also used : Page(com.almuradev.almura.feature.guide.Page) RemoteConnection(org.spongepowered.api.network.RemoteConnection) ClientboundPageListingsPacket(com.almuradev.almura.feature.guide.network.ClientboundPageListingsPacket) ServerPageManager(com.almuradev.almura.feature.guide.ServerPageManager) Scheduler(org.spongepowered.api.scheduler.Scheduler) Sponge(org.spongepowered.api.Sponge) ServerboundPageOpenRequestPacket(com.almuradev.almura.feature.guide.network.ServerboundPageOpenRequestPacket) NetworkConfig(com.almuradev.almura.shared.network.NetworkConfig) MessageHandler(org.spongepowered.api.network.MessageHandler) 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) ClientboundPageOpenResponsePacket(com.almuradev.almura.feature.guide.network.ClientboundPageOpenResponsePacket) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ClientboundPageOpenResponsePacket(com.almuradev.almura.feature.guide.network.ClientboundPageOpenResponsePacket) Player(org.spongepowered.api.entity.living.player.Player) 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)

Example 4 with Page

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

the class ClientboundPageOpenResponsePacket method readFrom.

@Override
public void readFrom(ChannelBuf buf) {
    // Page information
    final String id = buf.readString();
    final int index = buf.readInteger();
    final String name = buf.readString();
    final UUID creator = buf.readUniqueId();
    final Instant created = Instant.parse(buf.readString());
    final UUID lastModifier = buf.readUniqueId();
    final Instant lastModified = Instant.parse(buf.readString());
    String StrPacket1 = buf.readString();
    String StrPacket2 = buf.readString();
    String StrPacket3 = buf.readString();
    String StrPacket4 = buf.readString();
    final String content = StrPacket1 + StrPacket2 + StrPacket3 + StrPacket4;
    this.page = new Page(id, creator, created);
    this.page.setIndex(index);
    this.page.setName(name);
    this.page.setLastModifier(lastModifier);
    this.page.setLastModified(lastModified);
    this.page.setContent(content);
}
Also used : Instant(java.time.Instant) Page(com.almuradev.almura.feature.guide.Page) UUID(java.util.UUID)

Aggregations

Page (com.almuradev.almura.feature.guide.Page)4 PageListEntry (com.almuradev.almura.feature.guide.PageListEntry)3 ServerPageManager (com.almuradev.almura.feature.guide.ServerPageManager)3 ClientboundPageListingsPacket (com.almuradev.almura.feature.guide.network.ClientboundPageListingsPacket)3 NetworkConfig (com.almuradev.almura.shared.network.NetworkConfig)3 PacketUtil (com.almuradev.almura.shared.util.PacketUtil)3 Collectors (java.util.stream.Collectors)3 Inject (javax.inject.Inject)3 MinecraftServer (net.minecraft.server.MinecraftServer)3 Platform (org.spongepowered.api.Platform)3 Sponge (org.spongepowered.api.Sponge)3 Player (org.spongepowered.api.entity.living.player.Player)3 ChannelBinding (org.spongepowered.api.network.ChannelBinding)3 ChannelId (org.spongepowered.api.network.ChannelId)3 MessageHandler (org.spongepowered.api.network.MessageHandler)3 PlayerConnection (org.spongepowered.api.network.PlayerConnection)3 RemoteConnection (org.spongepowered.api.network.RemoteConnection)3 PluginContainer (org.spongepowered.api.plugin.PluginContainer)3 Instant (java.time.Instant)2 Scheduler (org.spongepowered.api.scheduler.Scheduler)2