RollingDice.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.isPlayer())
  32. {
  33. playable.sendPacket(SystemMessageId.ITEM_NOT_FOR_PETS);
  34. return false;
  35. }
  36. L2PcInstance activeChar = playable.getActingPlayer();
  37. int itemId = item.getItemId();
  38. if (activeChar.isInOlympiadMode())
  39. {
  40. activeChar.sendPacket(SystemMessageId.THIS_ITEM_IS_NOT_AVAILABLE_FOR_THE_OLYMPIAD_EVENT);
  41. return false;
  42. }
  43. int number = rollDice(activeChar);
  44. if (number == 0)
  45. {
  46. activeChar.sendPacket(SystemMessageId.YOU_MAY_NOT_THROW_THE_DICE_AT_THIS_TIME_TRY_AGAIN_LATER);
  47. return false;
  48. }
  49. Broadcast.toSelfAndKnownPlayers(activeChar, new Dice(activeChar.getObjectId(), itemId, number, activeChar.getX() - 30, activeChar.getY() - 30, activeChar.getZ()));
  50. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_ROLLED_S2);
  51. sm.addString(activeChar.getName());
  52. sm.addNumber(number);
  53. activeChar.sendPacket(sm);
  54. if (activeChar.isInsideZone(L2Character.ZONE_PEACE))
  55. {
  56. Broadcast.toKnownPlayers(activeChar, sm);
  57. }
  58. else if (activeChar.isInParty()) // TODO: Verify this!
  59. {
  60. activeChar.getParty().broadcastToPartyMembers(activeChar, sm);
  61. }
  62. return true;
  63. }
  64. /**
  65. * @param player
  66. * @return
  67. */
  68. private int rollDice(L2PcInstance player)
  69. {
  70. // Check if the dice is ready
  71. if (!player.getFloodProtectors().getRollDice().tryPerformAction("roll dice"))
  72. {
  73. return 0;
  74. }
  75. return Rnd.get(1, 6);
  76. }
  77. }