use of com.almuradev.almura.feature.store.network.ClientboundListItemsResponsePacket in project Almura by AlmuraDev.
the class ServerStoreManager method handleDelistBuyingItems.
public void handleDelistBuyingItems(final Player player, final String id, final List<Integer> recNos) {
checkNotNull(player);
checkNotNull(id);
checkNotNull(recNos);
checkState(!recNos.isEmpty());
if (!player.hasPermission(Almura.ID + ".store.admin")) {
this.notificationManager.sendPopupNotification(player, Text.of(TextColors.RED, "Store"), Text.of("You do not have permission " + "to unlist items!"), 5);
return;
}
final Store store = this.getStore(id).orElse(null);
if (store == null) {
this.logger.error("Player '{}' attempted to de-list buying items for store '{}' but the server has no knowledge of it. Syncing " + "store registry...", player.getName(), id);
this.syncStoreRegistryTo(player);
return;
}
final List<BuyingItem> buyingItems = store.getBuyingItems();
if (buyingItems.isEmpty()) {
this.logger.error("Player '{}' attempted to de-list buying items for store '{}' but the server knows of no " + " buying items for it. This could be a de-sync or an exploit.", player.getName(), store.getId());
this.network.sendTo(player, new ClientboundListItemsResponsePacket(store.getId(), StoreItemSegmentType.BUYING, buyingItems));
return;
}
final List<Integer> copyRecNos = Lists.newArrayList(recNos);
final Iterator<Integer> iter = copyRecNos.iterator();
while (iter.hasNext()) {
final Integer recNo = iter.next();
final BuyingItem found = buyingItems.stream().filter(v -> v.getRecord() == recNo).findAny().orElse(null);
if (found == null) {
this.logger.error("Player '{}' attempted to de-list buying item '{}' for store '{}' but the server knows of no knowledge of it. " + "This could be a de-sync or an exploit. Discarding...", player.getName(), recNo, store.getId());
iter.remove();
}
}
this.scheduler.createTaskBuilder().async().execute(() -> {
try (final DSLContext context = this.databaseManager.createContext(true)) {
final List<Query> toUpdate = new ArrayList<>();
for (final Integer recNo : recNos) {
toUpdate.add(StoreQueries.createUpdateBuyingItemIsHidden(recNo, true).build(context));
}
context.batch(toUpdate).execute();
final Results results = StoreQueries.createFetchBuyingItemsAndDataFor(store.getId(), false).build(context).fetchMany();
final List<BuyingItem> finalBuyingItems = new ArrayList<>();
results.forEach(result -> finalBuyingItems.addAll(this.parseBuyingItemsFor(result)));
this.scheduler.createTaskBuilder().execute(() -> {
store.putBuyingItems(finalBuyingItems);
this.network.sendToAll(new ClientboundListItemsResponsePacket(store.getId(), StoreItemSegmentType.BUYING, finalBuyingItems));
}).submit(this.container);
} catch (final SQLException e) {
e.printStackTrace();
}
}).submit(this.container);
}
use of com.almuradev.almura.feature.store.network.ClientboundListItemsResponsePacket in project Almura by AlmuraDev.
the class ServerStoreManager method openStoreSpecific.
void openStoreSpecific(final Player player, final Store store) {
checkNotNull(player);
checkNotNull(store);
if (!player.hasPermission(store.getPermission())) {
this.notificationManager.sendPopupNotification(player, Text.of(TextColors.RED, "Store"), Text.of("You do not have permission " + "to open this store!"), 5);
return;
}
this.network.sendTo(player, new ClientboundStoreGuiResponsePacket(StoreGuiType.SPECIFIC, store.getId(), player.hasPermission(Almura.ID + ".store.admin")));
if (!store.isLoaded()) {
this.playerSpecificInitiatorIds.add(player.getUniqueId());
this.databaseManager.getQueue().queue(DatabaseQueue.ActionType.FETCH_IGNORE_DUPLICATES, store.getId(), () -> {
this.loadSellingItems(store);
this.loadBuyingItems(store);
this.scheduler.createTaskBuilder().execute(() -> {
store.setLoaded(true);
final Iterator<UUID> iterator = this.playerSpecificInitiatorIds.iterator();
while (iterator.hasNext()) {
final UUID uniqueId = iterator.next();
iterator.remove();
final Player p = Sponge.getServer().getPlayer(uniqueId).orElse(null);
if (p != null && p.isOnline() && !p.isRemoved()) {
this.network.sendTo(p, new ClientboundListItemsResponsePacket(store.getId(), StoreItemSegmentType.BUYING, store.getBuyingItems()));
this.network.sendTo(p, new ClientboundListItemsResponsePacket(store.getId(), StoreItemSegmentType.SELLING, store.getSellingItems()));
}
}
}).submit(this.container);
});
} else {
this.network.sendTo(player, new ClientboundListItemsResponsePacket(store.getId(), StoreItemSegmentType.SELLING, store.getSellingItems()));
this.network.sendTo(player, new ClientboundListItemsResponsePacket(store.getId(), StoreItemSegmentType.BUYING, store.getBuyingItems()));
}
}
use of com.almuradev.almura.feature.store.network.ClientboundListItemsResponsePacket in project Almura by AlmuraDev.
the class ServerStoreManager method handleModifySellingItems.
public void handleModifySellingItems(final Player player, final String id, final List<ServerboundModifyItemsPacket.ModifyCandidate> candidates) {
checkNotNull(player);
checkNotNull(id);
checkNotNull(candidates);
if (!player.hasPermission(Almura.ID + ".store.admin")) {
this.notificationManager.sendPopupNotification(player, Text.of(TextColors.RED, "Store"), Text.of("You do not have permission " + "to modify listed items!"), 5);
return;
}
final Store store = this.getStore(id).orElse(null);
if (store == null) {
this.logger.error("Player '{}' attempted to modify selling items for store '{}' but the server has no knowledge of it. Syncing " + "store registry...", player.getName(), id);
this.syncStoreRegistryTo(player);
return;
}
final List<SellingItem> sellingItems = store.getSellingItems();
if (sellingItems.isEmpty()) {
this.logger.error("Player '{}' attempted to modify selling items for store '{}' but the server knows of no " + " selling items for it. This could be a de-sync or an exploit.", player.getName(), store.getId());
this.network.sendTo(player, new ClientboundListItemsResponsePacket(store.getId(), StoreItemSegmentType.SELLING, sellingItems));
return;
}
final List<ServerboundModifyItemsPacket.ModifyCandidate> copyCandidates = Lists.newArrayList(candidates);
final Iterator<ServerboundModifyItemsPacket.ModifyCandidate> iter = copyCandidates.iterator();
while (iter.hasNext()) {
final ServerboundModifyItemsPacket.ModifyCandidate candidate = iter.next();
final SellingItem found = sellingItems.stream().filter(v -> v.getRecord() == candidate.recNo).findAny().orElse(null);
if (found == null) {
this.logger.error("Player '{}' attempted to modify selling item '{}' for store '{}' but the server knows of no knowledge of it. " + "This could be a de-sync or an exploit. Discarding...", player.getName(), candidate.recNo, store.getId());
iter.remove();
}
}
this.scheduler.createTaskBuilder().async().execute(() -> {
try (final DSLContext context = this.databaseManager.createContext(true)) {
final List<Query> toUpdate = new ArrayList<>();
for (final ServerboundModifyItemsPacket.ModifyCandidate candidate : copyCandidates) {
toUpdate.add(StoreQueries.createUpdateSellingItem(candidate.recNo, candidate.quantity, candidate.index, candidate.price).build(context));
}
context.batch(toUpdate).execute();
final Results results = StoreQueries.createFetchSellingItemsAndDataFor(store.getId(), false).build(context).fetchMany();
final List<SellingItem> finalSellingItems = new ArrayList<>();
results.forEach(result -> finalSellingItems.addAll(this.parseSellingItemsFrom(result)));
this.scheduler.createTaskBuilder().execute(() -> {
store.putSellingItems(finalSellingItems);
this.network.sendToAll(new ClientboundListItemsResponsePacket(store.getId(), StoreItemSegmentType.SELLING, finalSellingItems));
}).submit(this.container);
} catch (final SQLException e) {
e.printStackTrace();
}
}).submit(this.container);
}
use of com.almuradev.almura.feature.store.network.ClientboundListItemsResponsePacket in project Almura by AlmuraDev.
the class ServerStoreManager method handleListSellingItems.
public void handleListSellingItems(final Player player, final String id, final List<ServerboundListItemsRequestPacket.ListCandidate> candidates) {
checkNotNull(player);
checkNotNull(id);
if (!player.hasPermission(Almura.ID + ".store.admin")) {
this.notificationManager.sendPopupNotification(player, Text.of(TextColors.RED, "Store"), Text.of("You do not have permission " + "to list items!"), 5);
return;
}
final Store store = this.getStore(id).orElse(null);
if (store == null) {
this.logger.error("Player '{}' attempted to list selling items for store '{}' but the server has no knowledge of it. Syncing " + "store registry...", player.getName(), id);
this.syncStoreRegistryTo(player);
return;
}
this.scheduler.createTaskBuilder().async().execute(() -> {
try (final DSLContext context = this.databaseManager.createContext(true)) {
final Map<StoreSellingItemRecord, VanillaStack> inserted = new HashMap<>();
for (final ServerboundListItemsRequestPacket.ListCandidate candidate : candidates) {
final VanillaStack stack = candidate.stack;
final int index = candidate.index;
final BigDecimal price = candidate.price;
final StoreSellingItemRecord itemRecord = StoreQueries.createInsertSellingItem(store.getId(), Instant.now(), stack.getItem(), stack.getQuantity(), stack.getMetadata(), index, price).build(context).fetchOne();
if (itemRecord == null) {
this.notificationManager.sendWindowMessage(player, Text.of("Store"), Text.of("Critical error encountered, check the server console for more details!"));
this.logger.error("Player '{}' submitted a new selling item for store '{}' to the database but it failed. " + "Discarding changes and printing stack...", player.getName(), id);
this.printStacksToConsole(Lists.newArrayList(stack));
continue;
}
final NBTTagCompound compound = stack.getCompound();
if (compound != null) {
StoreSellingItemDataRecord dataRecord = null;
try {
dataRecord = StoreQueries.createInsertSellingItemData(itemRecord.getRecNo(), compound).build(context).fetchOne();
} catch (final IOException e) {
e.printStackTrace();
}
if (dataRecord == null) {
this.notificationManager.sendWindowMessage(player, Text.of("Store"), Text.of("Critical error encountered, check the server console for more details!"));
this.logger.error("Player '{}' submitted data for selling item record '{}' for store '{}' but it failed. " + "Discarding changes...", player.getName(), itemRecord.getRecNo(), id);
StoreQueries.createDeleteSellingItem(itemRecord.getRecNo()).build(context).execute();
}
}
inserted.put(itemRecord, stack);
}
this.scheduler.createTaskBuilder().execute(() -> {
for (final Map.Entry<StoreSellingItemRecord, VanillaStack> entry : inserted.entrySet()) {
final StoreSellingItemRecord record = entry.getKey();
final VanillaStack stack = entry.getValue();
final BasicSellingItem item = new BasicSellingItem(record.getRecNo(), record.getCreated().toInstant(), stack.getItem(), stack.getQuantity(), stack.getMetadata(), record.getPrice(), record.getIndex(), stack.getCompound());
store.getSellingItems().add(item);
}
this.network.sendToAll(new ClientboundListItemsResponsePacket(store.getId(), StoreItemSegmentType.SELLING, store.getSellingItems()));
}).submit(this.container);
} catch (final SQLException e) {
e.printStackTrace();
}
}).submit(this.container);
}
use of com.almuradev.almura.feature.store.network.ClientboundListItemsResponsePacket in project Almura by AlmuraDev.
the class ServerStoreManager method handleModifyBuyingItems.
public void handleModifyBuyingItems(final Player player, final String id, final List<ServerboundModifyItemsPacket.ModifyCandidate> candidates) {
checkNotNull(player);
checkNotNull(id);
checkNotNull(candidates);
if (!player.hasPermission(Almura.ID + ".store.admin")) {
this.notificationManager.sendPopupNotification(player, Text.of(TextColors.RED, "Store"), Text.of("You do not have permission " + "to modify listed items!"), 5);
return;
}
final Store store = this.getStore(id).orElse(null);
if (store == null) {
this.logger.error("Player '{}' attempted to modify buying items for store '{}' but the server has no knowledge of it. Syncing " + "store registry...", player.getName(), id);
this.syncStoreRegistryTo(player);
return;
}
final List<BuyingItem> buyingItems = store.getBuyingItems();
if (buyingItems.isEmpty()) {
this.logger.error("Player '{}' attempted to modify buying items for store '{}' but the server knows of no " + " buying items for it. This could be a de-sync or an exploit.", player.getName(), store.getId());
this.network.sendTo(player, new ClientboundListItemsResponsePacket(store.getId(), StoreItemSegmentType.SELLING, buyingItems));
return;
}
final List<ServerboundModifyItemsPacket.ModifyCandidate> copyCandidates = Lists.newArrayList(candidates);
final Iterator<ServerboundModifyItemsPacket.ModifyCandidate> iter = copyCandidates.iterator();
while (iter.hasNext()) {
final ServerboundModifyItemsPacket.ModifyCandidate candidate = iter.next();
final BuyingItem found = buyingItems.stream().filter(v -> v.getRecord() == candidate.recNo).findAny().orElse(null);
if (found == null) {
this.logger.error("Player '{}' attempted to modify buying item '{}' for store '{}' but the server knows of no knowledge of it. " + "This could be a de-sync or an exploit. Discarding...", player.getName(), candidate.recNo, store.getId());
iter.remove();
}
}
this.scheduler.createTaskBuilder().async().execute(() -> {
try (final DSLContext context = this.databaseManager.createContext(true)) {
final List<Query> toUpdate = new ArrayList<>();
for (final ServerboundModifyItemsPacket.ModifyCandidate candidate : copyCandidates) {
toUpdate.add(StoreQueries.createUpdateBuyingItem(candidate.recNo, candidate.quantity, candidate.index, candidate.price).build(context));
}
context.batch(toUpdate).execute();
final Results results = StoreQueries.createFetchBuyingItemsAndDataFor(store.getId(), false).build(context).fetchMany();
final List<BuyingItem> finalBuyingItems = new ArrayList<>();
results.forEach(result -> finalBuyingItems.addAll(this.parseBuyingItemsFor(result)));
this.scheduler.createTaskBuilder().execute(() -> {
store.putBuyingItems(finalBuyingItems);
this.network.sendToAll(new ClientboundListItemsResponsePacket(store.getId(), StoreItemSegmentType.BUYING, finalBuyingItems));
}).submit(this.container);
} catch (final SQLException e) {
e.printStackTrace();
}
}).submit(this.container);
}
Aggregations