HiredMerchant.java
上传用户:gwt600
上传日期:2021-06-03
资源大小:704k
文件大小:4k
源码类别:

游戏

开发平台:

Java

  1. package net.sf.odinms.server.PlayerInteraction;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import java.util.concurrent.ScheduledFuture;
  6. import net.sf.odinms.client.IItem;
  7. import net.sf.odinms.client.MapleCharacter;
  8. import net.sf.odinms.client.MapleClient;
  9. import net.sf.odinms.database.DatabaseConnection;
  10. import net.sf.odinms.server.MapleInventoryManipulator;
  11. import net.sf.odinms.server.TimerManager;
  12. import net.sf.odinms.server.maps.MapleMap;
  13. import net.sf.odinms.server.maps.MapleMapObjectType;
  14. import net.sf.odinms.tools.MaplePacketCreator;
  15. /**
  16.  *
  17.  * @author XoticStory
  18.  */
  19. public class HiredMerchant extends PlayerInteractionManager {
  20.     private boolean open;
  21.     public ScheduledFuture<?> schedule = null;
  22.     private MapleMap map;
  23.     private int itemId;
  24.     public HiredMerchant(MapleCharacter owner, int itemId, String desc) {
  25.         super(owner, itemId % 10, desc, 3);
  26.         this.itemId = itemId;
  27.         this.map = owner.getMap();
  28.         this.schedule = TimerManager.getInstance().schedule(new Runnable() {
  29.             @Override
  30.             public void run() {
  31.                 HiredMerchant.this.closeShop(true);
  32.             }
  33.         }, 1000 * 60 * 60 * 24);
  34.     }
  35.     public byte getShopType() {
  36.         return IPlayerInteractionManager.HIRED_MERCHANT;
  37.     }
  38.     @Override
  39.     public void buy(MapleClient c, int item, short quantity) {
  40.         MaplePlayerShopItem pItem = items.get(item);
  41.         if (pItem.getBundles() > 0) {
  42.             synchronized (items) {
  43.                 IItem newItem = pItem.getItem().copy();
  44.                 newItem.setQuantity(quantity);
  45.                 if (c.getPlayer().getMeso() >= pItem.getPrice() * quantity) {
  46.                     if (MapleInventoryManipulator.addFromDrop(c, newItem)) {
  47.                         Connection con = DatabaseConnection.getConnection();
  48.                         try {
  49.                             PreparedStatement ps = con.prepareStatement("UPDATE characters SET MerchantMesos = MerchantMesos + " + pItem.getPrice() * quantity + " WHERE id = ?");
  50.                             ps.setInt(1, getOwnerId());
  51.                             ps.executeUpdate();
  52.                             ps.close();
  53.                         } catch (SQLException se) {
  54.                             se.printStackTrace();
  55.                         }
  56.                         c.getPlayer().gainMeso(-pItem.getPrice() * quantity, false);
  57.                         pItem.setBundles((short) (pItem.getBundles() - quantity));
  58.                     } else {
  59.                         c.getPlayer().dropMessage(1, "Your inventory is full.");
  60.                     }
  61.                 } else {
  62.                     c.getPlayer().dropMessage(1, "You do not have enough mesos.");
  63.                 }
  64.             }
  65.         }
  66.     }
  67.     @Override
  68.     public void closeShop(boolean saveItems) {
  69.         map.removeMapObject(this);
  70.         map.broadcastMessage(MaplePacketCreator.destroyHiredMerchant(getOwnerId()));
  71.         try {
  72.             PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("UPDATE characters SET HasMerchant = 0 WHERE id = ?");
  73.             ps.setInt(1, getOwnerId());
  74.             ps.executeUpdate();
  75.             ps.close();
  76.             if (saveItems) {
  77.                 saveItems();
  78.             }
  79.         } catch (SQLException se) {
  80.         }
  81.         schedule.cancel(false);
  82.     }
  83.     public boolean isOpen() {
  84.         return this.open;
  85.     }
  86.     public void setOpen(boolean set) {
  87.         this.open = set;
  88.     }
  89.     public MapleMap getMap() {
  90.         return map;
  91.     }
  92.     public int getItemId() {
  93.         return itemId;
  94.     }
  95.     @Override
  96.     public void sendDestroyData(MapleClient client) {
  97.         throw new UnsupportedOperationException();
  98.     }
  99.     @Override
  100.     public MapleMapObjectType getType() {
  101.         return MapleMapObjectType.HIRED_MERCHANT;
  102.     }
  103.     @Override
  104.     public void sendSpawnData(MapleClient client) {
  105.         client.getSession().write(MaplePacketCreator.spawnHiredMerchant(this));
  106.     }
  107. }