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

游戏

开发平台:

Java

  1. package net.sf.odinms.net.channel.handler;
  2. import java.util.Arrays;
  3. import net.sf.odinms.client.IItem;
  4. import net.sf.odinms.client.MapleCharacter;
  5. import net.sf.odinms.client.MapleClient;
  6. import net.sf.odinms.client.MapleInventoryType;
  7. import net.sf.odinms.client.messages.CommandProcessor;
  8. import net.sf.odinms.net.AbstractMaplePacketHandler;
  9. import net.sf.odinms.server.MapleInventoryManipulator;
  10. import net.sf.odinms.server.MapleItemInformationProvider;
  11. import net.sf.odinms.server.PlayerInteraction.MapleMiniGame;
  12. import net.sf.odinms.server.PlayerInteraction.MapleMiniGame.MiniGameType;
  13. import net.sf.odinms.server.PlayerInteraction.MaplePlayerShopItem;
  14. import net.sf.odinms.server.MapleTrade;
  15. import net.sf.odinms.server.maps.MapleMapObject;
  16. import net.sf.odinms.server.PlayerInteraction.PlayerInteractionManager;
  17. import net.sf.odinms.server.PlayerInteraction.HiredMerchant;
  18. import net.sf.odinms.server.PlayerInteraction.IPlayerInteractionManager;
  19. import net.sf.odinms.server.PlayerInteraction.MaplePlayerShop;
  20. import net.sf.odinms.server.maps.MapleMapObjectType;
  21. import net.sf.odinms.tools.MaplePacketCreator;
  22. import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;
  23. /**
  24.  *
  25.  * @author Matze
  26.  */
  27. public class PlayerInteractionHandler extends AbstractMaplePacketHandler {
  28.     private enum Action {
  29.         CREATE(0),
  30.         INVITE(2),
  31.         DECLINE(3),
  32.         VISIT(4),
  33.         CHAT(6),
  34.         EXIT(0xA),
  35.         OPEN(0xB),
  36.         SET_ITEMS(0xE),
  37.         SET_MESO(0xF),
  38.         CONFIRM(0x10),
  39.         ADD_ITEM(0x14),
  40.         BUY(0x15),
  41.         REMOVE_ITEM(0x19),
  42.         BAN_PLAYER(0x1A),
  43.         PUT_ITEM(0x1F),
  44.         MERCHANT_BUY(0x20),
  45.         TAKE_ITEM_BACK(0x24),
  46.         MAINTENANCE_OFF(0x25),
  47.         MERCHANT_ORGANIZE(0x26),
  48.         CLOSE_MERCHANT(0x27),
  49.         REQUEST_TIE(44),
  50.         ANSWER_TIE(45),
  51.         GIVE_UP(46),
  52.         EXIT_AFTER_GAME(50),
  53.         CANCEL_EXIT(51),
  54.         READY(52),
  55.         UN_READY(53),
  56.         MOVE_OMOK(58),
  57.         START(55),
  58.         SKIP(57),
  59.         SELECT_CARD(62);
  60.         final byte code;
  61.         private Action(int code) {
  62.             this.code = (byte) code;
  63.         }
  64.         public byte getCode() {
  65.             return code;
  66.         }
  67.     }
  68.     public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
  69.         System.out.println(slea);
  70.         byte mode = slea.readByte();
  71.         if (mode == Action.CREATE.getCode()) {
  72.             byte createType = slea.readByte();
  73.             if (createType == 3) { // trade
  74.                 MapleTrade.startTrade(c.getPlayer());
  75.             } else {
  76.                 if (c.getPlayer().getChalkboard() != null) {
  77.                     return;
  78.                 }
  79.                 if (createType == 1 || createType == 2) {
  80.                     String desc = slea.readMapleAsciiString();
  81.                     String pass = null;
  82.                     if (slea.readByte() == 1) {
  83.                         pass = slea.readMapleAsciiString();
  84.                     }
  85.                     int type = slea.readByte();
  86.                     IPlayerInteractionManager game = new MapleMiniGame(c.getPlayer(), type, desc);
  87.                     c.getPlayer().setInteraction(game);
  88.                     if (createType == 1) {
  89.                         ((MapleMiniGame) game).setGameType(MiniGameType.OMOK);
  90.                     } else if (createType == 2) {
  91.                         if (type == 0) {
  92.                             ((MapleMiniGame) game).setMatchesToWin(6);
  93.                         } else if (type == 1) {
  94.                             ((MapleMiniGame) game).setMatchesToWin(10);
  95.                         } else if (type == 2) {
  96.                             ((MapleMiniGame) game).setMatchesToWin(15);
  97.                         }
  98.                         ((MapleMiniGame) game).setGameType(MiniGameType.MATCH_CARDS);
  99.                     }
  100.                     c.getPlayer().getMap().addMapObject((PlayerInteractionManager) game);
  101.                     c.getPlayer().getMap().broadcastMessage(MaplePacketCreator.sendInteractionBox(c.getPlayer()));
  102.                 } else if (createType == 4 || createType == 5) { // shop
  103.                     if (c.getPlayer().getMap().getMapObjectsInRange(c.getPlayer().getPosition(), 19500, Arrays.asList(MapleMapObjectType.SHOP, MapleMapObjectType.HIRED_MERCHANT)).size() != 0) {
  104.                         c.getPlayer().dropMessage(1, "You may not establish a mini box here.");
  105.                         return;
  106.                     }
  107.                     String desc = slea.readMapleAsciiString();
  108.                     slea.skip(3);
  109.                     int itemId = slea.readInt();
  110.                     IPlayerInteractionManager shop;
  111.                     if (createType == 4) {
  112.                         shop = new MaplePlayerShop(c.getPlayer(), itemId, desc);
  113.                     } else {
  114.                         shop = new HiredMerchant(c.getPlayer(), itemId, desc);
  115.                     }
  116.                     c.getPlayer().setInteraction(shop);
  117.                     c.getSession().write(MaplePacketCreator.getInteraction(c.getPlayer(), true));
  118.                 } else {
  119.                     System.out.println("Unhandled PLAYER_INTERACTION packet: " + slea.toString());
  120.                 }
  121.             }
  122.         } else if (mode == Action.INVITE.getCode()) {
  123.             int otherPlayer = slea.readInt();
  124.             MapleCharacter otherChar = c.getPlayer().getMap().getCharacterById(otherPlayer);
  125.             MapleTrade.inviteTrade(c.getPlayer(), otherChar);
  126.         } else if (mode == Action.DECLINE.getCode()) {
  127.             MapleTrade.declineTrade(c.getPlayer());
  128.         } else if (mode == Action.VISIT.getCode()) {
  129.             // we will ignore the trade oids for now
  130.             if (c.getPlayer().getTrade() != null && c.getPlayer().getTrade().getPartner() != null) {
  131.                 MapleTrade.visitTrade(c.getPlayer(), c.getPlayer().getTrade().getPartner().getChr());
  132.             } else {
  133.                 int oid = slea.readInt();
  134.                 MapleMapObject ob = c.getPlayer().getMap().getMapObject(oid);
  135.                 if (ob instanceof IPlayerInteractionManager && c.getPlayer().getInteraction() == null) {
  136.                     IPlayerInteractionManager ips = (IPlayerInteractionManager) ob;
  137.                     if (ips.getShopType() == 1) {
  138.                         HiredMerchant merchant = (HiredMerchant) ips;
  139.                         if (merchant.isOwner(c.getPlayer())) {
  140.                             merchant.setOpen(false);
  141.                             merchant.broadcast(MaplePacketCreator.shopErrorMessage(0x0D, 1), false);
  142.                             merchant.removeAllVisitors((byte) 16, (byte) 0);
  143.                             c.getPlayer().setInteraction(ips);
  144.                             c.getSession().write(MaplePacketCreator.getInteraction(c.getPlayer(), false));
  145.                             return;
  146.                         } else if (!merchant.isOpen()) {
  147.                             c.getPlayer().dropMessage(1, "This shop is in maintenance, please come by later.");
  148.                             return;
  149.                         }
  150.                     } else if (ips.getShopType() == 2) {
  151.                         if (((MaplePlayerShop) ips).isBanned(c.getPlayer().getName())) {
  152.                             c.getPlayer().dropMessage(1, "You have been banned from this store.");
  153.                             return;
  154.                         }
  155.                     }
  156.                     if (ips.getFreeSlot() == -1) {
  157.                         c.getSession().write(MaplePacketCreator.getMiniBoxFull());
  158.                         return;
  159.                     }
  160.                     c.getPlayer().setInteraction(ips);
  161.                     ips.addVisitor(c.getPlayer());
  162.                     c.getSession().write(MaplePacketCreator.getInteraction(c.getPlayer(), false));
  163.                 }
  164.             }
  165.         } else if (mode == Action.CHAT.getCode()) { // chat lol
  166.             if (c.getPlayer().getTrade() != null) {
  167.                 c.getPlayer().getTrade().chat(slea.readMapleAsciiString());
  168.             } else if (c.getPlayer().getInteraction() != null) {
  169.                 IPlayerInteractionManager ips = c.getPlayer().getInteraction();
  170.                 String message = slea.readMapleAsciiString();
  171.                 CommandProcessor.getInstance().processCommand(c, message); // debug purposes
  172.                 ips.broadcast(MaplePacketCreator.shopChat(c.getPlayer().getName() + " : " + message, ips.isOwner(c.getPlayer()) ? 0 : ips.getVisitorSlot(c.getPlayer()) + 1), true);
  173.             }
  174.         } else if (mode == Action.EXIT.getCode()) {
  175.             if (c.getPlayer().getTrade() != null) {
  176.                 MapleTrade.cancelTrade(c.getPlayer());
  177.             } else {
  178.                 IPlayerInteractionManager ips = c.getPlayer().getInteraction();
  179.                 c.getPlayer().setInteraction(null);
  180.                 if (ips != null) {
  181.                     if (ips.isOwner(c.getPlayer())) {
  182.                         if (ips.getShopType() == 2) {
  183.                             boolean save = false;
  184.                             for (MaplePlayerShopItem items : ips.getItems()) {
  185.                                 if (items.getBundles() > 0) {
  186.                                     IItem item = items.getItem();
  187.                                     item.setQuantity(items.getBundles());
  188.                                     if (MapleInventoryManipulator.addFromDrop(c, item)) {
  189.                                         items.setBundles((short) 0);
  190.                                     } else {
  191.                                         save = true;
  192.                                         break;
  193.                                     }
  194.                                 }
  195.                             }
  196.                             ips.removeAllVisitors(3, 1);
  197.                             ips.closeShop(save);
  198.                         } else if (ips.getShopType() == 1) {
  199.                             c.getSession().write(MaplePacketCreator.shopVisitorLeave(0));
  200.                         } else if (ips.getShopType() == 3 || ips.getShopType() == 4) {
  201.                             ips.removeAllVisitors(3, 1);
  202.                         }
  203.                     } else {
  204.                         ips.removeVisitor(c.getPlayer());
  205.                     }
  206.                 }
  207.             }
  208.         } else if (mode == Action.OPEN.getCode()) {
  209.             IPlayerInteractionManager shop = c.getPlayer().getInteraction();
  210.             if (shop != null && shop.isOwner(c.getPlayer())) {
  211.                 c.getPlayer().getMap().addMapObject((PlayerInteractionManager) shop);
  212.                 if (shop.getShopType() == 1) {
  213.                     HiredMerchant merchant = (HiredMerchant) shop;
  214.                     c.getPlayer().setHasMerchant(true);
  215.                     merchant.setOpen(true);
  216.                     c.getPlayer().getMap().broadcastMessage(MaplePacketCreator.spawnHiredMerchant(merchant));
  217.                     c.getPlayer().setInteraction(null);
  218.                 } else if (shop.getShopType() == 2) {
  219.                     c.getPlayer().getMap().broadcastMessage(MaplePacketCreator.sendInteractionBox(c.getPlayer()));
  220.                 }
  221.                 slea.readByte();
  222.             }
  223.         } else if (mode == Action.READY.getCode()) {
  224.             c.getPlayer().getInteraction().broadcast(MaplePacketCreator.getMiniGameReady(), true);
  225.         } else if (mode == Action.UN_READY.getCode()) {
  226.             c.getPlayer().getInteraction().broadcast(MaplePacketCreator.getMiniGameUnReady(), true);
  227.         } else if (mode == Action.START.getCode()) {
  228.             MapleMiniGame game = (MapleMiniGame) c.getPlayer().getInteraction();
  229.             if (game.getGameType() == MiniGameType.OMOK) {
  230.                 game.broadcast(MaplePacketCreator.getMiniGameStart(game.getLoser()), true);
  231.             } else if (game.getGameType() == MiniGameType.MATCH_CARDS) {
  232.                 game.shuffleList();
  233.                 game.broadcast(MaplePacketCreator.getMatchCardStart(game), true);
  234.             }
  235.             c.getPlayer().getMap().broadcastMessage(MaplePacketCreator.sendInteractionBox(game.getOwner()));
  236.             game.setStarted(true);
  237.         } else if (mode == Action.GIVE_UP.getCode()) {
  238.             MapleMiniGame game = (MapleMiniGame) c.getPlayer().getInteraction();
  239.             if (game.getGameType() == MiniGameType.OMOK) {
  240.                 game.broadcast(MaplePacketCreator.getMiniGameForfeit(game, game.isOwner(c.getPlayer()) ? 0 : 1), true);
  241.             } else if (game.getGameType() == MiniGameType.MATCH_CARDS) {
  242.                 if (game.isOwner(c.getPlayer())) {
  243.                     game.broadcast(MaplePacketCreator.getMiniGameWin(game, 1), true);
  244.                 } else {
  245.                     game.broadcast(MaplePacketCreator.getMiniGameWin(game, 0), true);
  246.                 }
  247.             }
  248.         } else if (mode == Action.REQUEST_TIE.getCode()) {
  249.             MapleMiniGame game = (MapleMiniGame) c.getPlayer().getInteraction();
  250.             if (game.isOwner(c.getPlayer())) {
  251.                 game.getVisitors()[0].getClient().getSession().write(MaplePacketCreator.getMiniGameRequestTie());
  252.             } else {
  253.                 game.getOwner().getClient().getSession().write(MaplePacketCreator.getMiniGameRequestTie());
  254.             }
  255.         } else if (mode == Action.ANSWER_TIE.getCode()) {
  256.             MapleMiniGame game = (MapleMiniGame) c.getPlayer().getInteraction();
  257.             if (slea.readByte() == 1) {
  258.                 game.broadcast(MaplePacketCreator.getMiniGameTie(game), true);
  259.             } else {
  260.                 game.broadcast(MaplePacketCreator.getMiniGameDenyTie(), true);
  261.             }
  262.         } else if (mode == Action.SKIP.getCode()) {
  263.             IPlayerInteractionManager game = c.getPlayer().getInteraction();
  264.             game.broadcast(MaplePacketCreator.getMiniGameSkipTurn(game.isOwner(c.getPlayer()) ? 0 : 1), true);
  265.         } else if (mode == Action.MOVE_OMOK.getCode()) {
  266.             int x = slea.readInt(); // x point
  267.             int y = slea.readInt(); // y point
  268.             int type = slea.readByte(); // piece ( 1 or 2; Owner has one piece, visitor has another, it switches every game.)
  269.             ((MapleMiniGame) c.getPlayer().getInteraction()).setPiece(x, y, type, c.getPlayer());
  270.         } else if (mode == Action.SELECT_CARD.getCode()) {
  271.             int turn = slea.readByte(); // 1st turn = 1; 2nd turn = 0
  272.             int slot = slea.readByte(); // slot
  273.             MapleMiniGame game = (MapleMiniGame) c.getPlayer().getInteraction();
  274.             int firstslot = game.getFirstSlot();
  275.             if (turn == 1) {
  276.                 game.setFirstSlot(slot);
  277.                 game.broadcast(MaplePacketCreator.getMatchCardSelect(turn, slot, firstslot, turn), !game.isOwner(c.getPlayer()));
  278.             } else if (game.getCardId(firstslot + 1) == game.getCardId(slot + 1)) {
  279.                 if (game.isOwner(c.getPlayer())) {
  280.                     game.broadcast(MaplePacketCreator.getMatchCardSelect(turn, slot, firstslot, 2), true);
  281.                     game.setOwnerPoints();
  282.                 } else {
  283.                     game.broadcast(MaplePacketCreator.getMatchCardSelect(turn, slot, firstslot, 3), true);
  284.                     game.setVisitorPoints();
  285.                 }
  286.             } else {
  287.                 game.broadcast(MaplePacketCreator.getMatchCardSelect(turn, slot, firstslot, game.isOwner(c.getPlayer()) ? 0 : 1), true);
  288.             }
  289.         } else if (mode == Action.SET_MESO.getCode()) {
  290.             c.getPlayer().getTrade().setMeso(slea.readInt());
  291.         } else if (mode == Action.SET_ITEMS.getCode()) {
  292.             MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
  293.             MapleInventoryType ivType = MapleInventoryType.getByType(slea.readByte());
  294.             IItem item = c.getPlayer().getInventory(ivType).getItem((byte) slea.readShort());
  295.             short quantity = slea.readShort();
  296.             byte targetSlot = slea.readByte();
  297.             if (c.getPlayer().getTrade() != null) {
  298.                 if ((quantity <= item.getQuantity() && quantity >= 0) || ii.isThrowingStar(item.getItemId()) || ii.isBullet(item.getItemId())) {
  299.                     if (!c.getChannelServer().allowUndroppablesDrop() && ii.isDropRestricted(item.getItemId())) { // ensure that undroppable items do not make it to the trade window
  300.                         c.getSession().write(MaplePacketCreator.enableActions());
  301.                         return;
  302.                     }
  303.                     IItem tradeItem = item.copy();
  304.                     if (ii.isThrowingStar(item.getItemId()) || ii.isBullet(item.getItemId())) {
  305.                         tradeItem.setQuantity(item.getQuantity());
  306.                         MapleInventoryManipulator.removeFromSlot(c, ivType, item.getPosition(), item.getQuantity(), true);
  307.                     } else {
  308.                         tradeItem.setQuantity(quantity);
  309.                         MapleInventoryManipulator.removeFromSlot(c, ivType, item.getPosition(), quantity, true);
  310.                     }
  311.                     tradeItem.setPosition(targetSlot);
  312.                     c.getPlayer().getTrade().addItem(tradeItem);
  313.                     return;
  314.                 }
  315.             }
  316.         } else if (mode == Action.CONFIRM.getCode()) {
  317.             MapleTrade.completeTrade(c.getPlayer());
  318.         } else if (mode == Action.ADD_ITEM.getCode() || mode == Action.PUT_ITEM.getCode()) {
  319.             MapleInventoryType type = MapleInventoryType.getByType(slea.readByte());
  320.             byte slot = (byte) slea.readShort();
  321.             short bundles = slea.readShort();
  322.             short perBundle = slea.readShort();
  323.             int price = slea.readInt();
  324.             IItem ivItem = c.getPlayer().getInventory(type).getItem(slot);
  325.             IItem sellItem = ivItem.copy();
  326.             sellItem.setQuantity(perBundle);
  327.             MaplePlayerShopItem item = new MaplePlayerShopItem(sellItem, bundles, price);
  328.             IPlayerInteractionManager shop = c.getPlayer().getInteraction();
  329.             if (shop != null && shop.isOwner(c.getPlayer())) {
  330.                 if (ivItem != null && ivItem.getQuantity() >= bundles * perBundle) {
  331.                     MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
  332.                     if (ii.isThrowingStar(ivItem.getItemId()) || ii.isBullet(ivItem.getItemId())) {
  333.                         MapleInventoryManipulator.removeFromSlot(c, type, slot, ivItem.getQuantity(), true);
  334.                     } else {
  335.                         MapleInventoryManipulator.removeFromSlot(c, type, slot, (short) (bundles * perBundle), true);
  336.                     }
  337.                     shop.addItem(item);
  338.                     c.getSession().write(MaplePacketCreator.shopItemUpdate(shop));
  339.                 }
  340.             }
  341.         } else if (mode == Action.BUY.getCode() || mode == Action.MERCHANT_BUY.getCode()) {
  342.             int item = slea.readByte();
  343.             short quantity = slea.readShort();
  344.             IPlayerInteractionManager shop = c.getPlayer().getInteraction();
  345.             shop.buy(c, item, quantity);
  346.             shop.broadcast(MaplePacketCreator.shopItemUpdate(shop), true);
  347.         } else if (mode == Action.TAKE_ITEM_BACK.getCode() || mode == Action.REMOVE_ITEM.getCode()) {
  348.             int slot = slea.readShort();
  349.             IPlayerInteractionManager shop = c.getPlayer().getInteraction();
  350.             if (shop != null) {
  351.                 MaplePlayerShopItem item = shop.getItems().get(slot);
  352.                 if (item.getBundles() > 0) {
  353.                     IItem iitem = item.getItem();
  354.                     iitem.setQuantity(item.getBundles());
  355.                     MapleInventoryManipulator.addFromDrop(c, iitem);
  356.                 }
  357.                 shop.removeFromSlot(slot);
  358.                 c.getSession().write(MaplePacketCreator.shopItemUpdate(shop));
  359.             }
  360.         } else if (mode == Action.CLOSE_MERCHANT.getCode()) {
  361.             IPlayerInteractionManager merchant = c.getPlayer().getInteraction();
  362.             if (merchant != null && merchant.getShopType() == 1 && merchant.isOwner(c.getPlayer())) {
  363.                 boolean save = false;
  364.                 for (MaplePlayerShopItem items : merchant.getItems()) {
  365.                     if (items.getBundles() > 0) {
  366.                         IItem item = items.getItem();
  367.                         item.setQuantity(items.getBundles());
  368.                         if (MapleInventoryManipulator.addFromDrop(c, item)) {
  369.                             items.setBundles((short) 0);
  370.                         } else {
  371.                             save = true;
  372.                             break;
  373.                         }
  374.                     }
  375.                 }
  376.                 c.getSession().write(MaplePacketCreator.shopErrorMessage(0x10, 0));
  377.                 merchant.closeShop(save);
  378.                 c.getPlayer().setInteraction(null);
  379.                 c.getPlayer().setHasMerchant(false);
  380.             }
  381.         } else if (mode == Action.MAINTENANCE_OFF.getCode()) {
  382.             HiredMerchant merchant = (HiredMerchant) c.getPlayer().getInteraction();
  383.             if (merchant != null && merchant.isOwner(c.getPlayer())) {
  384.                 merchant.setOpen(true);
  385.             }
  386.         } else if (mode == Action.BAN_PLAYER.getCode()) {
  387.             IPlayerInteractionManager imps = c.getPlayer().getInteraction();
  388.             if (imps != null && imps.isOwner(c.getPlayer())) {
  389.                 ((MaplePlayerShop) imps).banPlayer(slea.readMapleAsciiString());
  390.             }
  391.         } else if (mode == Action.MERCHANT_ORGANIZE.getCode()) {
  392.             IPlayerInteractionManager imps = c.getPlayer().getInteraction();
  393.             for (int i = 0; i < imps.getItems().size(); i++) {
  394.                 if (imps.getItems().get(i).getBundles() == 0) {
  395.                     imps.removeFromSlot(i);
  396.                 }
  397.             }
  398.             c.getSession().write(MaplePacketCreator.shopItemUpdate(imps));
  399.         }
  400.     }
  401. }