RollingDice.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 handlers.itemhandlers;
  16. import com.l2jserver.gameserver.handler.IItemHandler;
  17. import com.l2jserver.gameserver.model.actor.L2Character;
  18. import com.l2jserver.gameserver.model.actor.L2Playable;
  19. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  20. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  21. import com.l2jserver.gameserver.network.SystemMessageId;
  22. import com.l2jserver.gameserver.network.serverpackets.Dice;
  23. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  24. import com.l2jserver.gameserver.util.Broadcast;
  25. import com.l2jserver.util.Rnd;
  26. public class RollingDice implements IItemHandler
  27. {
  28. @Override
  29. public boolean useItem(L2Playable playable, L2ItemInstance item, boolean forceUse)
  30. {
  31. if (!(playable instanceof L2PcInstance))
  32. return false;
  33. L2PcInstance activeChar = (L2PcInstance) playable;
  34. int itemId = item.getItemId();
  35. if (activeChar.isInOlympiadMode())
  36. {
  37. activeChar.sendPacket(SystemMessageId.THIS_ITEM_IS_NOT_AVAILABLE_FOR_THE_OLYMPIAD_EVENT);
  38. return false;
  39. }
  40. if (itemId == 4625 || itemId == 4626 || itemId == 4627 || itemId == 4628)
  41. {
  42. int number = rollDice(activeChar);
  43. if (number == 0)
  44. {
  45. activeChar.sendPacket(SystemMessageId.YOU_MAY_NOT_THROW_THE_DICE_AT_THIS_TIME_TRY_AGAIN_LATER);
  46. return false;
  47. }
  48. Broadcast.toSelfAndKnownPlayers(activeChar, new Dice(activeChar.getObjectId(), item.getItemId(), number, activeChar.getX() - 30, activeChar.getY() - 30, activeChar.getZ()));
  49. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_ROLLED_S2);
  50. sm.addString(activeChar.getName());
  51. sm.addNumber(number);
  52. activeChar.sendPacket(sm);
  53. if (activeChar.isInsideZone(L2Character.ZONE_PEACE))
  54. Broadcast.toKnownPlayers(activeChar, sm);
  55. else if (activeChar.isInParty())
  56. activeChar.getParty().broadcastToPartyMembers(activeChar, sm);
  57. return true;
  58. }
  59. return false;
  60. }
  61. /**
  62. * @param player
  63. * @return
  64. */
  65. private int rollDice(L2PcInstance player)
  66. {
  67. // Check if the dice is ready
  68. if (!player.getFloodProtectors().getRollDice().
  69. tryPerformAction("roll dice"))
  70. {
  71. return 0;
  72. }
  73. return Rnd.get(1, 6);
  74. }
  75. }