use of com.almuradev.almura.feature.guide.network.ServerboundPageChangeRequestPacket 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)));
}
}
}
Aggregations