View Javadoc

1   /***
2    * Jetrix TetriNET Server
3    * Copyright (C) 2004  Gamereplay (game@gamereplay.be)
4    * Copyright (C) 2004  Emmanuel Bourg
5    *
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19   */
20  
21  package net.jetrix.commands;
22  
23  import java.util.Locale;
24  
25  import net.jetrix.messages.channel.CommandMessage;
26  import net.jetrix.messages.channel.PlineMessage;
27  import net.jetrix.config.*;
28  import net.jetrix.*;
29  
30  /***
31   * Switch between preconfigured settings.
32   *
33   * @author Gamereplay
34   * @author Emmanuel Bourg
35   * @version $Revision: 846 $, $Date: 2010-05-03 17:29:44 +0200 (lun., 03 mai 2010) $
36   */
37  public class ModeCommand extends AbstractCommand
38  {
39      private static int[][] modes =
40              {
41                  {15, 14, 14, 14, 14, 14, 14, 1, 0},
42                  {10, 15, 15, 15, 15, 15, 15, 1, 1},
43                  {20, 14, 13, 13, 13, 13, 14, 1, 0},
44                  {10, 15, 15, 15, 15, 15, 15, 2, 1},
45                  {25, 12, 13, 13, 12, 12, 13, 1, 0},
46                  {0,  17, 17, 17, 16, 16, 17, 1, 0},
47                  {100, 0,  0,  0,  0,  0,  0, 1, 0},
48                  {0,   0,  0,  0, 50, 50,  0, 1, 1},
49                  {0,   0, 50, 50,  0,  0,  0, 1, 1},
50                  {0,   0,  0,  0,  0,  0, 100,1, 1}
51              };
52  
53      static
54      {
55          Language.getInstance().addResources("command.mode");
56      }
57  
58      public String getAlias()
59      {
60          return "mode";
61      }
62  
63      public String getUsage(Locale locale)
64      {
65          return "/" + getAlias() + " <0-" + (modes.length - 1) + ">";
66      }
67  
68      public void updateSetting(Settings settings, int[] mode)
69      {
70          for (Block block : Block.values())
71          {
72              settings.setOccurancy(block, mode[block.ordinal()]);
73          }
74  
75          settings.setLinesPerSpecial(mode[7]);
76          settings.setSpecialAdded(mode[8]);
77      }
78  
79      public void execute(CommandMessage message)
80      {
81          Client client = (Client) message.getSource();
82          Channel channel = client.getChannel();
83  
84          if (message.getParameterCount() == 0)
85          {
86              Locale locale = client.getUser().getLocale();
87  
88              // display all modes available
89              for (int i = 0; i < modes.length; i++)
90              {
91                  Message tmode = new PlineMessage("<red>/" + getAlias() + " <aqua>" + i + "</aqua> : <darkBlue>" + Language.getText("command.mode.message" + i, locale));
92                  client.send(tmode);
93              }
94          }
95          else
96          {
97              int param = -1;
98              
99              try
100             {
101                 param = Integer.parseInt(message.getParameter(0));
102             }
103             catch (NumberFormatException e) { }
104             
105             if (param >= 0 && param < modes.length)
106             {
107                 updateSetting(channel.getConfig().getSettings(), modes[param]);
108 
109                 PlineMessage enabled = new PlineMessage();
110                 enabled.setKey("command.mode.enabled", "key:command.mode.message" + param);
111                 channel.send(enabled);
112             }
113             else
114             {
115                 PlineMessage error = new PlineMessage();
116                 error.setText("<red>/" + getAlias() + "</red> <blue><0-" + (modes.length - 1) + "></blue>");
117                 client.send(error);
118             }
119         }
120     }
121 }