use of com.almuradev.almura.feature.store.listing.BuyingItem 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.listing.BuyingItem in project Almura by AlmuraDev.
the class ServerStoreManager method parseBuyingItemsFor.
private List<BuyingItem> parseBuyingItemsFor(final Result<Record> result) {
final List<BuyingItem> items = new ArrayList<>();
for (final Record record : result) {
final Integer recNo = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.REC_NO);
final String domain = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.DOMAIN);
final String path = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.PATH);
final ResourceLocation location = new ResourceLocation(domain, path);
final Item item = ForgeRegistries.ITEMS.getValue(location);
if (item == null) {
this.logger.error("Unknown buying item for domain '{}' and path '{}' found at record number '{}'. Skipping... (Did you remove a " + "mod?)", domain, path, recNo);
continue;
}
final Timestamp created = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.CREATED);
final Integer quantity = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.QUANTITY);
final Integer metadata = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.METADATA);
final BigDecimal price = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.PRICE);
final Integer index = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.INDEX);
final byte[] compoundData = record.getValue(StoreBuyingItemData.STORE_BUYING_ITEM_DATA.DATA);
NBTTagCompound compound = null;
if (compoundData != null) {
try {
compound = SerializationUtil.compoundFromBytes(compoundData);
} catch (final IOException e) {
this.logger.error("Malformed buying item data found at record number '{}'. Skipping...", recNo);
continue;
}
}
final BasicBuyingItem basicBuyingItem = new BasicBuyingItem(recNo, created.toInstant(), item, quantity, metadata, price, index, compound);
items.add(basicBuyingItem);
}
return items;
}
use of com.almuradev.almura.feature.store.listing.BuyingItem 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);
}
use of com.almuradev.almura.feature.store.listing.BuyingItem in project Almura by AlmuraDev.
the class ServerStoreManager method loadBuyingItems.
/**
* Buying Items
*/
private void loadBuyingItems(final Store store) {
checkNotNull(store);
this.logger.info("Querying buying items for store '{}' ({}), please wait...", store.getName(), store.getId());
final List<BuyingItem> items = new ArrayList<>();
try (final DSLContext context = this.databaseManager.createContext(true)) {
final Results results = StoreQueries.createFetchBuyingItemsAndDataFor(store.getId(), false).build(context).keepStatement(false).fetchMany();
results.forEach(result -> items.addAll(this.parseBuyingItemsFor(result)));
store.clearBuyingItems();
items.sort(Comparator.comparingInt(BuyingItem::getIndex));
store.putBuyingItems(items.isEmpty() ? null : items);
this.logger.info("Loaded [{}] buying item(s) for store '{}' ({}).", items.size(), store.getName(), store.getId());
} catch (final SQLException e) {
e.printStackTrace();
}
}
use of com.almuradev.almura.feature.store.listing.BuyingItem in project Almura by AlmuraDev.
the class StoreScreen method updateAdminControls.
private void updateAdminControls() {
if (!this.isAdmin) {
return;
}
final ItemStack selectedItem = this.adminItemList.getSelectedItem();
this.buttonAdminList.setEnabled(selectedItem != null);
final Optional<BuyingItem> buyingItem = this.store.getBuyingItems().stream().filter(i -> StoreScreen.isStackEqualIgnoreSize(i.asRealStack(), selectedItem)).findAny();
final Optional<SellingItem> sellingItem = this.store.getSellingItems().stream().filter(i -> StoreScreen.isStackEqualIgnoreSize(i.asRealStack(), selectedItem)).findAny();
this.buttonAdminUnlist.setEnabled(buyingItem.isPresent() || sellingItem.isPresent());
final String status = (buyingItem.isPresent() || sellingItem.isPresent()) ? "modify" : "list";
this.buttonAdminList.setText(I18n.format("almura.feature.common.button." + status));
this.adminItemList.getComponents().stream().filter(i -> i instanceof AdminItemComponent).forEach(i -> ((AdminItemComponent) i).update());
this.adminListTotalLabel.setText(TextFormatting.WHITE + I18n.format("almura.feature.common.text.total") + ": " + this.adminItemList.getItems().size());
}
Aggregations