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()));
}
}
}
}
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)));
}
}
}
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));
}
}
}
}
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);
}
Aggregations