Get shop as Optional
This commit is contained in:
@@ -36,6 +36,6 @@ public class GmShopCommand extends Command {
|
||||
|
||||
@Override
|
||||
public void execute(Client c, String[] params, CommandContext ctx) {
|
||||
ctx.shopFactory().getShop(GM_SHOP_ID).sendShop(c);
|
||||
ctx.shopFactory().getShop(GM_SHOP_ID).ifPresent(shop -> shop.sendShop(c));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@ import net.packet.InPacket;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import scripting.npc.NPCScriptManager;
|
||||
import server.Shop;
|
||||
import server.ShopFactory;
|
||||
import server.life.NPC;
|
||||
import server.life.PlayerNPC;
|
||||
@@ -106,14 +105,10 @@ public final class NPCTalkHandler extends AbstractPacketHandler {
|
||||
}
|
||||
|
||||
private boolean hasShop(NPC npc) {
|
||||
return shopFactory.getShop(npc.getId()) != null;
|
||||
return shopFactory.getShop(npc.getId()).isPresent();
|
||||
}
|
||||
|
||||
private void sendShop(NPC npc, Client c) {
|
||||
Shop shop = shopFactory.getShop(npc.getId());
|
||||
if (shop == null) {
|
||||
return;
|
||||
}
|
||||
shop.sendShop(c);
|
||||
shopFactory.getShop(npc.getId()).ifPresent(shop -> shop.sendShop(c));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ import tools.Pair;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.DAYS;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
@@ -511,9 +512,9 @@ public final class UseCashItemHandler extends AbstractPacketHandler {
|
||||
}
|
||||
} else if (itemType == 545) { // MiuMiu's travel store
|
||||
if (player.getShop() == null) {
|
||||
Shop shop = shopFactory.getShop(MIU_MIU_SHOP_ID);
|
||||
if (shop != null) {
|
||||
shop.sendShop(c);
|
||||
Optional<Shop> shop = shopFactory.getShop(MIU_MIU_SHOP_ID);
|
||||
if (shop.isPresent()) {
|
||||
shop.get().sendShop(c);
|
||||
remove(c, position, itemId);
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -37,11 +37,12 @@ import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author Matze
|
||||
* @author Ponk
|
||||
*/
|
||||
public class ShopFactory {
|
||||
private static final short MAX_QUANTITY_PER_PURCHASE = 1000; // Should really use max stack size for the given item
|
||||
private static final Set<Integer> rechargeableItemIds = rechargeableItemIds();
|
||||
private final Cache<Integer, Shop> shops = Caffeine.newBuilder().build();
|
||||
private final Cache<Integer, Optional<Shop>> shops = Caffeine.newBuilder().build();
|
||||
private final ShopDao shopDao;
|
||||
|
||||
public ShopFactory(ShopDao shopDao) {
|
||||
@@ -56,17 +57,17 @@ public class ShopFactory {
|
||||
.collect(Collectors.toUnmodifiableSet());
|
||||
}
|
||||
|
||||
public Shop getShop(int shopId) {
|
||||
public Optional<Shop> getShop(int shopId) {
|
||||
return shops.get(shopId, this::loadShop);
|
||||
}
|
||||
|
||||
private Shop loadShop(int shopId) {
|
||||
private Optional<Shop> loadShop(int shopId) {
|
||||
Optional<database.shop.Shop> dbShop = shopDao.getShop(shopId);
|
||||
if (dbShop.isEmpty()) {
|
||||
throw new IllegalArgumentException("Shop with id %d does not exist".formatted(shopId));
|
||||
return Optional.empty();
|
||||
}
|
||||
List<ShopItem> items = shopDao.getShopItems(shopId);
|
||||
return new Shop(dbShop.get().id(), dbShop.get().npcId(), fromDbShopItems(items));
|
||||
return Optional.of(new Shop(dbShop.get().id(), dbShop.get().npcId(), fromDbShopItems(items)));
|
||||
}
|
||||
|
||||
private List<server.ShopItem> fromDbShopItems(List<ShopItem> dbItems) {
|
||||
|
||||
Reference in New Issue
Block a user