Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.settings;
24 
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32 import org.jetbrains.annotations.NotNull;
33 
38 public class Macros {
39 
43  @NotNull
44  private static final String REPLY_TO = "reply_to";
45 
49  @NotNull
50  private final Pattern macroPattern = Pattern.compile("<<([a-z_]+)>>");
51 
55  @NotNull
56  private final Map<String, String> expansions = new HashMap<>();
57 
61  @NotNull
62  @SuppressWarnings("FieldCanBeLocal")
64 
65  @Override
66  public void commandDrawextinfoReceived(final int color, final int type, final int subtype, @NotNull final String message) {
67  switch (type) {
82  break;
83 
86  final int index = message.indexOf(" tells you:");
87  if (index != -1) {
88  final String name = message.substring(0, index);
89  expansions.put(REPLY_TO, name);
90  }
91  }
92  break;
93 
99  default:
100  break;
101  }
102  }
103 
104  @Override
105  public void setDebugMode(final boolean printMessageTypes) {
106  // ignore
107  }
108 
109  };
110 
116  public Macros(@NotNull final CrossfireServerConnection crossfireServerConnection) {
117  expansions.put(REPLY_TO, "");
118  crossfireServerConnection.addCrossfireDrawextinfoListener(crossfireDrawextinfoListener);
119  }
120 
126  @NotNull
127  public String expandMacros(@NotNull final String string) {
128  StringBuilder result = null;
129  int index = 0;
130  final Matcher macroMatcher = macroPattern.matcher(string);
131  while (macroMatcher.find()) {
132  if (result == null) {
133  result = new StringBuilder();
134  }
135 
136  final String name = macroMatcher.group(1);
137  String expansion = expansions.get(name);
138  if (expansion == null) {
139  expansion = macroMatcher.group(); // do not expand unknown macro names
140  }
141  result.append(string, index, macroMatcher.start());
142  result.append(expansion);
143  index = macroMatcher.end();
144  }
145  if (result != null) {
146  result.append(string.substring(index));
147  }
148  return result == null ? string : result.toString();
149  }
150 
151 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.server
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_MONUMENT
static final int MSG_TYPE_MONUMENT
drawextinfo message type: character did read a monument.
Definition: MessageType.java:58
com.realtime.crossfire.jxclient.settings.Macros.REPLY_TO
static final String REPLY_TO
The "reply_to" macro name.
Definition: Macros.java:44
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
Definition: CrossfireServerConnection.java:37
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_SKILL
static final int MSG_TYPE_SKILL
drawextinfo message type: message related to using skills.
Definition: MessageType.java:94
com.realtime.crossfire.jxclient.settings.Macros.Macros
Macros(@NotNull final CrossfireServerConnection crossfireServerConnection)
Creates a new instance.
Definition: Macros.java:116
com.realtime.crossfire.jxclient.settings.Macros.macroPattern
final Pattern macroPattern
The Pattern matching macro names.
Definition: Macros.java:50
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_MOTD
static final int MSG_TYPE_MOTD
drawextinfo message type: motd text.
Definition: MessageType.java:68
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_BOOK
static final int MSG_TYPE_BOOK
drawextinfo message type: character did read a book.
Definition: MessageType.java:38
com.realtime.crossfire.jxclient.protocol.MessageType
Encapsulates the message type numbers for drawextinfo messages.
Definition: MessageType.java:33
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_SIGN
static final int MSG_TYPE_SIGN
drawextinfo message type: character did read a sign.
Definition: MessageType.java:53
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_CARD
static final int MSG_TYPE_CARD
drawextinfo message type: character did read a card.
Definition: MessageType.java:43
com.realtime.crossfire.jxclient.protocol
Definition: MagicMap.java:23
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_APPLY
static final int MSG_TYPE_APPLY
drawextinfo message type: an object was applied.
Definition: MessageType.java:99
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_COMMAND
static final int MSG_TYPE_COMMAND
drawextinfo message type: response to command processing.
Definition: MessageType.java:83
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_MISC
static final int MSG_TYPE_MISC
drawextinfo message type: message that does not fit in any other category.
Definition: MessageType.java:125
com.realtime.crossfire.jxclient.settings.Macros
Manages macro expansion in command strings.
Definition: Macros.java:38
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_ADMIN
static final int MSG_TYPE_ADMIN
drawextinfo message type: general server message.
Definition: MessageType.java:73
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_CLIENT
static final int MSG_TYPE_CLIENT
drawextinfo message type: client originated messages.
Definition: MessageType.java:141
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_ATTRIBUTE
static final int MSG_TYPE_ATTRIBUTE
drawextinfo message type: attribute (stats, resistances, etc.) change message.
Definition: MessageType.java:89
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_DIALOG
static final int MSG_TYPE_DIALOG
drawextinfo message type: a NPC/magic mouth/altar/etc.
Definition: MessageType.java:63
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_PAPER
static final int MSG_TYPE_PAPER
drawextinfo message type: character did read a paper.
Definition: MessageType.java:48
com.realtime.crossfire.jxclient.settings.Macros.crossfireDrawextinfoListener
final CrossfireDrawextinfoListener crossfireDrawextinfoListener
The CrossfireDrawextinfoListener for tracking tells.
Definition: Macros.java:63
com.realtime.crossfire.jxclient.server.crossfire
Definition: AbstractCrossfireServerConnection.java:23
com.realtime.crossfire.jxclient.server.crossfire.CrossfireDrawextinfoListener
Interface for listeners interested in drawextinfo messages received from the Crossfire server.
Definition: CrossfireDrawextinfoListener.java:33
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_ATTACK
static final int MSG_TYPE_ATTACK
drawextinfo message type: attack related message.
Definition: MessageType.java:104
com.realtime.crossfire
com.realtime
com
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_SUBTYPE_COMMUNICATION_TELL
static final int MSG_SUBTYPE_COMMUNICATION_TELL
Definition: MessageType.java:405
com.realtime.crossfire.jxclient.settings.Macros.expandMacros
String expandMacros(@NotNull final String string)
Expands all macro references.
Definition: Macros.java:127
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_ITEM
static final int MSG_TYPE_ITEM
drawextinfo message type: item related information.
Definition: MessageType.java:119
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_SHOP
static final int MSG_TYPE_SHOP
drawextinfo message type: shop related message.
Definition: MessageType.java:78
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_SPELL
static final int MSG_TYPE_SPELL
drawextinfo message type: spell related information.
Definition: MessageType.java:114
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_COMMUNICATION
static final int MSG_TYPE_COMMUNICATION
drawextinfo message type: communication between players.
Definition: MessageType.java:109
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_VICTIM
static final int MSG_TYPE_VICTIM
drawextinfo message type: something bad is happening to the player.
Definition: MessageType.java:130
com.realtime.crossfire.jxclient.settings.Macros.expansions
final Map< String, String > expansions
The macro expansions.
Definition: Macros.java:56