RollingDice.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.handler.itemhandlers;
  16. import net.sf.l2j.gameserver.handler.IItemHandler;
  17. import net.sf.l2j.gameserver.model.L2Character;
  18. import net.sf.l2j.gameserver.model.L2ItemInstance;
  19. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  20. import net.sf.l2j.gameserver.model.actor.instance.L2PlayableInstance;
  21. import net.sf.l2j.gameserver.network.SystemMessageId;
  22. import net.sf.l2j.gameserver.network.serverpackets.Dice;
  23. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  24. import net.sf.l2j.gameserver.util.Broadcast;
  25. import net.sf.l2j.gameserver.util.FloodProtector;
  26. import net.sf.l2j.util.Rnd;
  27. /**
  28. * This class ...
  29. *
  30. * @version $Revision: 1.1.4.2 $ $Date: 2005/03/27 15:30:07 $
  31. */
  32. public class RollingDice implements IItemHandler
  33. {
  34. private static final int[] ITEM_IDS =
  35. {
  36. 4625, 4626, 4627, 4628
  37. };
  38. /**
  39. *
  40. * @see net.sf.l2j.gameserver.handler.IItemHandler#useItem(net.sf.l2j.gameserver.model.actor.instance.L2PlayableInstance, net.sf.l2j.gameserver.model.L2ItemInstance)
  41. */
  42. public void useItem(L2PlayableInstance playable, L2ItemInstance item)
  43. {
  44. if (!(playable instanceof L2PcInstance))
  45. return;
  46. L2PcInstance activeChar = (L2PcInstance) playable;
  47. int itemId = item.getItemId();
  48. if (activeChar.isInOlympiadMode())
  49. {
  50. activeChar.sendPacket(new SystemMessage(SystemMessageId.THIS_ITEM_IS_NOT_AVAILABLE_FOR_THE_OLYMPIAD_EVENT));
  51. return;
  52. }
  53. if (itemId == 4625 || itemId == 4626 || itemId == 4627 || itemId == 4628)
  54. {
  55. int number = rollDice(activeChar);
  56. if (number == 0)
  57. {
  58. activeChar.sendPacket(new SystemMessage(SystemMessageId.YOU_MAY_NOT_THROW_THE_DICE_AT_THIS_TIME_TRY_AGAIN_LATER));
  59. return;
  60. }
  61. Dice d = new Dice(activeChar.getObjectId(), item.getItemId(), number, activeChar.getX() - 30, activeChar.getY() - 30, activeChar.getZ());
  62. Broadcast.toSelfAndKnownPlayers(activeChar, d);
  63. SystemMessage sm = new SystemMessage(SystemMessageId.S1_ROLLED_S2);
  64. sm.addString(activeChar.getName());
  65. sm.addNumber(number);
  66. activeChar.sendPacket(sm);
  67. if (activeChar.isInsideZone(L2Character.ZONE_PEACE))
  68. Broadcast.toKnownPlayers(activeChar, sm);
  69. else if (activeChar.isInParty())
  70. activeChar.getParty().broadcastToPartyMembers(activeChar, sm);
  71. }
  72. }
  73. /**
  74. *
  75. * @param player
  76. * @return
  77. */
  78. private int rollDice(L2PcInstance player)
  79. {
  80. // Check if the dice is ready
  81. if (!FloodProtector.getInstance().tryPerformAction(player.getObjectId(), FloodProtector.PROTECTED_ROLLDICE))
  82. return 0;
  83. return Rnd.get(1, 6);
  84. }
  85. /**
  86. *
  87. * @see net.sf.l2j.gameserver.handler.IItemHandler#getItemIds()
  88. */
  89. public int[] getItemIds()
  90. {
  91. return ITEM_IDS;
  92. }
  93. }