1 20 21 package freecs.commands; 22 import freecs.interfaces.ICommand; 23 import freecs.interfaces.IGroupState; 24 import freecs.interfaces.IUserStates; 25 import freecs.content.MessageState; 26 import freecs.Server; 27 28 33 public class CmdHitDice extends AbstractCommand { 34 public final String cmd= "/td"; 35 private static final ICommand selve= new CmdHitDice(); 36 37 private CmdHitDice () { } 38 39 public static ICommand getInstance () { 40 return selve; 41 } 42 43 public boolean execute (MessageState msgState, String param) { 44 if (isPunished (msgState)) 45 return false; 46 msgState.targetGroup = msgState.sender.getGroup (); 47 if (msgState.targetGroup.hasState(IGroupState.MODERATED) || msgState.targetGroup.hasState(IGroupState.DEACTIVATE_HITDICE)) { 48 msgState.msgTemplate = "error.noRight.deactivated"; 49 msgState.sender.sendMessage(msgState.mp); 50 return false; 51 } else if (msgState.targetGroup.hasState(IGroupState.ENTRANCE) 52 && !msgState.sender.hasRight (IUserStates.ROLE_GOD)) { 53 msgState.msgTemplate = "error.noRight.noAdmin"; 54 msgState.sender.sendMessage(msgState.mp); 55 return false; 56 } else if (param.length () < 1) { 57 msgState.msgTemplate = "error.td.noArg"; 58 msgState.sender.sendMessage (msgState.mp); 59 return false; 60 } 61 msgState.reason = param; 62 param = param.toLowerCase (); 63 int posd = param.indexOf ("d"); 64 int poss = param.indexOf (" "); 65 int dies,eyes = 6; 66 if (posd > -1 || poss > -1) { 67 String values[]; 68 if (posd > -1) values = param.split ("d"); 69 else values = param.split (" "); 70 if (values.length > 2 || values.length < 1) { 71 msgState.msgTemplate = "error.td.wrong"; 72 msgState.sender.sendMessage (msgState.mp); 73 return false; 74 } 75 try { 76 dies = Integer.parseInt (values[0]); 77 eyes = Integer.parseInt (values[1]); 78 } catch (NumberFormatException nfe) { 79 msgState.msgTemplate = "error.td.wrong"; 80 msgState.sender.sendMessage (msgState.mp); 81 return false; 82 } 83 } else { 84 try { 85 dies = Integer.parseInt (param); 86 } catch (NumberFormatException nfe) { 87 msgState.msgTemplate = "error.td.wrong"; 88 msgState.sender.sendMessage (msgState.mp); 89 return false; 90 } 91 } 92 if (dies < 1 || eyes < 2) { 93 msgState.msgTemplate = "error.td.wrong"; 94 msgState.sender.sendMessage (msgState.mp); 95 return false; 96 } else if (dies > Server.srv.MAX_DIE_NUMBER || eyes > Server.srv.MAX_DIE_EYES) { 97 msgState.msgTemplate = "error.td.toomany"; 98 msgState.sender.sendMessage (msgState.mp); 99 return false; 100 } 101 if (dies > 1) { 102 StringBuffer sb = new StringBuffer (); 103 int total = 0; 104 for (int i = 0; i < dies; i++) { 105 sb.append (i+1); 106 sb.append (".: <b>"); 107 int rnd = (int) Math.round (Math.random () * (eyes -1)) + 1; 108 sb.append (rnd); 109 total += rnd; 110 if (i < dies-1) 111 sb.append ("</b> / "); 112 else 113 sb.append ("</b>"); 114 } 115 msgState.param = String.valueOf (total); 116 msgState.message = sb.toString (); 117 } else { 118 StringBuffer sb = new StringBuffer (); 119 int points = (int) Math.round (Math.random () * (eyes -1)) + 1; 120 msgState.param = String.valueOf (points); 121 msgState.message = String.valueOf (points); 122 } 123 msgState.msgTemplate = "message.td"; 124 msgState.sender.getGroup ().sendMessage (msgState.mp); 125 return true; 126 } 127 } 128 | Popular Tags |