Skip to content

Commit

Permalink
Implement new Admin feature to Exchange. Allows for the ability to de…
Browse files Browse the repository at this point in the history
…list other players items.

Signed-off-by: Dockter <dockter@almuramc.com>
  • Loading branch information
Dockter committed Dec 30, 2023
1 parent 5c3c17a commit 8276345
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
public enum ListStatusType {
LIST,
DE_LIST,
ADJUST_PRICE
ADJUST_PRICE,
ADMIN_DE_LIST
}
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,113 @@ public void handleListForSaleItem(final Player player, final String id, final in
.submit(this.container);
}

public void handleAdminDelistForSaleItem(final Player player, final String id, final int listItemRecNo) {
checkNotNull(player);
checkNotNull(id);
checkState(listItemRecNo >= 0);

final Exchange axs = this.getExchange(id).orElse(null);
if (axs == null) {
this.notificationManager.sendWindowMessage(player, Text.of("Exchange"), Text.of("Critical error encountered, check the "
+ "server console for more details!"));
this.logger.error("Admin '{}' attempted to de-list list item '{}' for exchange '{}' but the server has no knowledge of it. Syncing "
+ "exchange registry...", player.getName(), listItemRecNo, id);
this.syncExchangeRegistryTo(player);
return;
}

if (!player.hasPermission(axs.getPermission() +".admin")) {
return;
}

ListItem findItem = null;

for (final Map.Entry<UUID, List<ListItem>> kv : axs.getListItems().entrySet()) {
final ListItem listItem = kv.getValue().stream().filter(item -> item.getRecord() == listItemRecNo).findAny().orElse(null);
if (listItem != null) {
findItem = listItem;
break;
}
}

final ListItem found = findItem;

if (found == null) {
this.notificationManager.sendWindowMessage(player, Text.of("Exchange"), Text.of("Critical error encountered, check the "
+ "server console for more details!"));
this.logger.error("Admin '{}' attempted to de-list list item '{}' for exchange '{}' but the server has no record of the listing. "
+ "Syncing list items...", player.getName(), listItemRecNo, id);
return;
}

final ForSaleItem forSaleItem = found.getForSaleItem().orElse(null);
final List<ForSaleItem> sellerForSaleItems = axs.getForSaleItemsFor(found.getSeller()).orElse(null);

if (forSaleItem == null) {
this.notificationManager.sendWindowMessage(player, Text.of("Exchange"), Text.of("Critical error encountered, check the "
+ "server console for more details!"));
this.logger.error("Admin '{}' attempted to de-list list item '{}' for exchange '{}' but the server doesn't have a listing for "
+ "that item. Syncing list items sale status...", player.getName(), listItemRecNo, id);
return;
}

if (sellerForSaleItems == null) {
this.notificationManager.sendWindowMessage(player, Text.of("Exchange"), Text.of("Critical error encountered, check the "
+ "server console for more details!"));
this.logger.error("Admin '{}' attempted to de-list list item '{}' for exchange '{}' but the server has no record of any listings for "
+ "that player. Syncing list items sale status...", player.getName(), listItemRecNo, id);
this.network.sendTo(player, new ClientboundListItemsSaleStatusPacket(id, null, null));
return;
}

this.scheduler
.createTaskBuilder()
.async()
.execute(() -> {
try (final DSLContext context = this.databaseManager.createContext(true)) {
final int result = ExchangeQueries
.createUpdateForSaleItemIsHidden(forSaleItem.getRecord(), true)
.build(context)
.keepStatement(false)
.execute();

if (result == 0) {
this.logger.error("Admin '{}' attempted to de-list list item '{}' for exchange '{}' to the database but it failed. "
+ "Discarding changes...", player.getName(), listItemRecNo, id);
return;
}

ExchangeQueries
.createUpdateListItemLastKnownPrice(listItemRecNo, forSaleItem.getPrice())
.build(context)
.execute();

this.scheduler
.createTaskBuilder()
.execute(() -> {
found.setLastKnownPrice(forSaleItem.getPrice());

sellerForSaleItems.remove(forSaleItem);

found.setForSaleItem(null);
this.logger.info("Admin '{}' delisted item '{}' for exchange '{}'", player.getName(), listItemRecNo, id);

// Send the updated list to the client if for some reason the seller and player are the same person.
if (found.getSeller() == player.getUniqueId()) {
this.network.sendTo(player, new ClientboundListItemsSaleStatusPacket(axs.getId(), sellerForSaleItems, null));
}

this.network.sendToAll(new ClientboundForSaleFilterRequestPacket(axs.getId()));
})
.submit(this.container);
} catch (SQLException e) {
e.printStackTrace();
}
})
.submit(this.container);

}

public void handleDelistForSaleItem(final Player player, final String id, final int listItemRecNo) {
checkNotNull(player);
checkNotNull(id);
Expand Down Expand Up @@ -1252,10 +1359,10 @@ public void handleTransaction(final Player player, final String id, final int li
final UUID seller = found.getSeller();
final UUID buyer = player.getUniqueId();

if (buyer.equals(seller)) {
this.notificationManager.sendWindowMessage(player, Text.of("Exchange"), Text.of("You cannot purchase your own items."));
return;
}
//if (buyer.equals(seller)) {
// this.notificationManager.sendWindowMessage(player, Text.of("Exchange"), Text.of("You cannot purchase your own items."));
// return;
//}

final UniqueAccount sellerAccount = economyService.getOrCreateAccount(seller).orElse(null);
if (sellerAccount == null) {
Expand Down Expand Up @@ -1527,7 +1634,7 @@ private int getListingsLimit(final Player player, final Exchange axs) {
int slots = 0;
String axsBasePermission = axs.getPermission();

if (player.hasPermission(axsBasePermission + "admin")) {
if (player.hasPermission(axsBasePermission + ".admin")) {
return 1000000;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,16 @@ public void construct() {
this.itemDisplayNameSearchBox.focus();
}

@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
super.drawScreen(mouseX, mouseY, partialTicks);
if (isCtrlKeyDown()) {
this.buttonBuyOne.setText(I18n.format("almura.feature.common.button.delist"));
} else {
this.buttonBuyOne.setText(I18n.format("almura.feature.common.button.buy.one"));
}
}

@Override
public boolean doesGuiPauseGame() {
return false; // Can't stop the game otherwise the Sponge Scheduler also stops.
Expand Down Expand Up @@ -416,7 +426,11 @@ private void search() {
private void buy(final int value) {
final ForSaleItem forSaleItem = this.forSaleList.getSelectedItem();
if (forSaleItem != null) {
exchangeManager.purchase(this.axs.getId(), forSaleItem.getListItem().getRecord(), value);
if (isCtrlKeyDown()) {
exchangeManager.modifyListStatus(ListStatusType.ADMIN_DE_LIST, this.axs.getId(), forSaleItem.getListItem().getRecord(), null);
} else {
exchangeManager.purchase(this.axs.getId(), forSaleItem.getListItem().getRecord(), value);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
*/
package com.almuradev.almura.feature.exchange.network;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import com.almuradev.almura.feature.exchange.ListStatusType;
import com.almuradev.almura.shared.util.ByteBufUtil;
import com.almuradev.almura.shared.util.SerializationUtil;
import io.netty.buffer.ByteBuf;
import org.spongepowered.api.network.ChannelBuf;
import org.spongepowered.api.network.Message;

import javax.annotation.Nullable;
import java.math.BigDecimal;

import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

public final class ServerboundModifyForSaleItemListStatusRequestPacket implements Message {

Expand All @@ -41,7 +39,7 @@ public ServerboundModifyForSaleItemListStatusRequestPacket(final ListStatusType
this.listItemRecNo = listItemRecNo;
this.type = type;

if (this.type != ListStatusType.DE_LIST) {
if (this.type == ListStatusType.LIST || this.type == ListStatusType.ADJUST_PRICE) {
checkNotNull(price);
checkState(price.doubleValue() >= 0);
this.price = price;
Expand All @@ -54,7 +52,7 @@ public void readFrom(final ChannelBuf buf) {
this.id = buf.readString();
this.listItemRecNo = buf.readInteger();

if (this.type != ListStatusType.DE_LIST) {
if (this.type == ListStatusType.LIST || this.type == ListStatusType.ADJUST_PRICE) {
this.price = ByteBufUtil.readBigDecimal((ByteBuf) buf);
}
}
Expand All @@ -69,7 +67,7 @@ public void writeTo(final ChannelBuf buf) {
buf.writeString(this.id);
buf.writeInteger(this.listItemRecNo);

if (this.type != ListStatusType.DE_LIST) {
if (this.type == ListStatusType.LIST || this.type == ListStatusType.ADJUST_PRICE) {
checkNotNull(this.price);
checkState(this.price.doubleValue() >= 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*/
package com.almuradev.almura.feature.exchange.network.handler;

import static com.google.common.base.Preconditions.checkNotNull;

import com.almuradev.almura.feature.exchange.ServerExchangeManager;
import com.almuradev.almura.feature.exchange.network.ServerboundModifyForSaleItemListStatusRequestPacket;
import com.almuradev.almura.shared.util.PacketUtil;
Expand All @@ -23,6 +21,8 @@
import javax.inject.Inject;
import javax.inject.Singleton;

import static com.google.common.base.Preconditions.checkNotNull;

@Singleton
public final class ServerboundModifyForSaleItemListStatusRequestPacketHandler
implements MessageHandler<ServerboundModifyForSaleItemListStatusRequestPacket> {
Expand Down Expand Up @@ -54,6 +54,9 @@ public void handleMessage(final ServerboundModifyForSaleItemListStatusRequestPac
case ADJUST_PRICE:
this.exchangeManager.handleAdjustPriceForSaleItem(player, message.id, message.listItemRecNo, message.price);
break;
case ADMIN_DE_LIST:
this.exchangeManager.handleAdminDelistForSaleItem(player, message.id, message.listItemRecNo);
break;
default:
throw new UnsupportedOperationException(message.type + " is not supported yet!");
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/almura/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ almura.feature.common.button.sell.stack=Sell %d
almura.feature.common.button.sell=Sell
almura.feature.common.button.modify=Modify
almura.feature.common.button.unlist=Unlist
almura.feature.common.button.delist=Delist
almura.feature.common.text.add_feature=Are you sure you want to add this %s?
almura.feature.common.text.admin=Admin
almura.feature.common.text.created=Created
Expand Down

0 comments on commit 8276345

Please sign in to comment.