Crossfire JXClient, Trunk  R20561
Macros.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.settings;
23 
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31 import org.jetbrains.annotations.NotNull;
32 
37 public class Macros {
38 
42  @NotNull
43  private static final String REPLY_TO = "reply_to";
44 
48  @NotNull
49  private final Pattern macroPattern = Pattern.compile("<<([a-z_]+)>>");
50 
54  @NotNull
55  private final Map<String, String> expansions = new HashMap<>();
56 
60  @NotNull
61  @SuppressWarnings("FieldCanBeLocal")
63 
64  @Override
65  public void commandDrawextinfoReceived(final int color, final int type, final int subtype, @NotNull final String message) {
66  switch (type) {
81  break;
82 
85  final int index = message.indexOf(" tells you:");
86  if (index != -1) {
87  final String name = message.substring(0, index);
88  expansions.put(REPLY_TO, name);
89  }
90  }
91  break;
92 
97  default:
98  break;
99  }
100  }
101 
102  @Override
103  public void setDebugMode(final boolean printMessageTypes) {
104  // ignore
105  }
106 
107  };
108 
114  public Macros(@NotNull final CrossfireServerConnection crossfireServerConnection) {
115  expansions.put(REPLY_TO, "");
116  crossfireServerConnection.addCrossfireDrawextinfoListener(crossfireDrawextinfoListener);
117  }
118 
124  @NotNull
125  public String expandMacros(@NotNull final String string) {
126  StringBuilder result = null;
127  int index = 0;
128  final Matcher macroMatcher = macroPattern.matcher(string);
129  while (macroMatcher.find()) {
130  if (result == null) {
131  result = new StringBuilder();
132  }
133 
134  final String name = macroMatcher.group(1);
135  String expansion = expansions.get(name);
136  if (expansion == null) {
137  expansion = macroMatcher.group(); // do not expand unknown macro names
138  }
139  result.append(string.substring(index, macroMatcher.start()));
140  result.append(expansion);
141  index = macroMatcher.end();
142  }
143  if (result != null) {
144  result.append(string.substring(index, string.length()));
145  }
146  return result == null ? string : result.toString();
147  }
148 
149 }
static final int MSG_TYPE_SKILL
drawextinfo message type: message related to using skills.
Encapsulates the message type numbers for drawextinfo messages.
static final int MSG_TYPE_CARD
drawextinfo message type: character did read a card.
static final int MSG_TYPE_MISC
drawextinfo message type: message that does not fit in any other category.
final Pattern macroPattern
The Pattern matching macro names.
Definition: Macros.java:49
final Map< String, String > expansions
The macro expansions.
Definition: Macros.java:55
static final int MSG_TYPE_MONUMENT
drawextinfo message type: character did read a monument.
static final int MSG_TYPE_PAPER
drawextinfo message type: character did read a paper.
String expandMacros(@NotNull final String string)
Expands all macro references.
Definition: Macros.java:125
final CrossfireDrawextinfoListener crossfireDrawextinfoListener
The CrossfireDrawextinfoListener for tracking tells.
Definition: Macros.java:62
static final int MSG_TYPE_COMMAND
drawextinfo message type: response to command processing.
Manages macro expansion in command strings.
Definition: Macros.java:37
static final int MSG_TYPE_ADMIN
drawextinfo message type: general server message.
static final int MSG_TYPE_ATTACK
drawextinfo message type: attack related message.
static final int MSG_TYPE_APPLY
drawextinfo message type: an object was applied.
static final String REPLY_TO
The "reply_to" macro name.
Definition: Macros.java:43
static final int MSG_TYPE_SHOP
drawextinfo message type: shop related message.
static final int MSG_TYPE_SIGN
drawextinfo message type: character did read a sign.
static final int MSG_TYPE_ITEM
drawextinfo message type: item related information.
Interface for listeners interested in drawextinfo messages received from the Crossfire server...
static final int MSG_TYPE_COMMUNICATION
drawextinfo message type: communication between players.
static final int MSG_TYPE_MOTD
drawextinfo message type: motd text.
static final int MSG_TYPE_SPELL
drawextinfo message type: spell related information.
Macros(@NotNull final CrossfireServerConnection crossfireServerConnection)
Creates a new instance.
Definition: Macros.java:114
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
static final int MSG_TYPE_DIALOG
drawextinfo message type: a NPC/magic mouth/altar/etc.
static final int MSG_TYPE_ATTRIBUTE
drawextinfo message type: attribute (stats, resistances, etc.) change message.
static final int MSG_TYPE_VICTIM
drawextinfo message type: something bad is happening to the player.
static final int MSG_TYPE_BOOK
drawextinfo message type: character did read a book.