Crossfire JXClient, Trunk  R20561
ScriptManager.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.scripts;
23 
33 import java.io.IOException;
34 import java.util.Collection;
35 import java.util.HashSet;
36 import java.util.Set;
37 import java.util.concurrent.CopyOnWriteArraySet;
38 import java.util.stream.Collectors;
39 import org.jetbrains.annotations.NotNull;
40 
45 public class ScriptManager {
46 
50  @NotNull
51  private final CommandQueue commandQueue;
52 
56  @NotNull
58 
62  @NotNull
63  private final Stats stats;
64 
68  @NotNull
69  private final FloorView floorView;
70 
74  @NotNull
75  private final ItemSet itemSet;
76 
80  @NotNull
81  private final Iterable<Spell> spellsManager;
82 
86  @NotNull
88 
92  @NotNull
93  private final SkillSet skillSet;
94 
98  @NotNull
99  private final Collection<ScriptProcess> scriptProcesses = new CopyOnWriteArraySet<>();
100 
104  private int nextScriptId = 1;
105 
117  public ScriptManager(@NotNull final CommandQueue commandQueue, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final Stats stats, @NotNull final FloorView floorView, @NotNull final ItemSet itemSet, @NotNull final Iterable<Spell> spellsManager, @NotNull final MapUpdaterState mapUpdaterState, @NotNull final SkillSet skillSet) {
118  this.commandQueue = commandQueue;
119  this.crossfireServerConnection = crossfireServerConnection;
120  this.stats = stats;
121  this.floorView = floorView;
122  this.itemSet = itemSet;
123  this.spellsManager = spellsManager;
124  this.mapUpdaterState = mapUpdaterState;
125  this.skillSet = skillSet;
126  }
127 
132  public void newScript(@NotNull final String command) {
133  final DefaultScriptProcess scriptProcess;
134  try {
135  scriptProcess = new DefaultScriptProcess(nextScriptId, command, commandQueue, crossfireServerConnection, stats, floorView, itemSet, spellsManager, mapUpdaterState, skillSet);
136  } catch (final IOException ex) {
137  crossfireServerConnection.drawInfo("Unable to run script: "+ex.getMessage(), CrossfireDrawinfoListener.NDI_RED);
138  return;
139  }
140  nextScriptId++;
141  scriptProcesses.add(scriptProcess);
142  scriptProcess.addScriptProcessListener(result -> {
143  scriptProcesses.remove(scriptProcess);
144  if (result == null) {
145  crossfireServerConnection.drawInfo("Script '"+scriptProcess+"' finished.", CrossfireDrawinfoListener.NDI_BLACK);
146  } else {
147  crossfireServerConnection.drawInfo("Script '"+scriptProcess+"' failed: "+result, CrossfireDrawinfoListener.NDI_RED);
148  }
149  });
150  crossfireServerConnection.drawInfo("Script '"+scriptProcess+"' started.", CrossfireDrawinfoListener.NDI_BLACK);
151  new Thread(scriptProcess, "JXClient:ScriptProcess:"+scriptProcess).start();
152  }
153 
161  @NotNull
162  public Set<ScriptProcess> getScripts(@NotNull final String partialScriptName) {
163  try {
164  return getScriptByScriptId(Integer.parseInt(partialScriptName));
165  } catch (final NumberFormatException ignored) {
166  return getScriptsByName(partialScriptName);
167  }
168  }
169 
175  @NotNull
176  private Set<ScriptProcess> getScriptByScriptId(final int scriptId) {
177  final Set<ScriptProcess> result = new HashSet<>();
178  for (final ScriptProcess scriptProcess : scriptProcesses) {
179  if (scriptProcess.getScriptId() == scriptId) {
180  result.add(scriptProcess);
181  break;
182  }
183  }
184  return result;
185  }
186 
193  @NotNull
194  private Set<ScriptProcess> getScriptsByName(@NotNull final CharSequence partialScriptName) {
195  return scriptProcesses.stream().filter(scriptProcess -> scriptProcess.getFilename().contains(partialScriptName)).collect(Collectors.toSet());
196  }
197 
202  public boolean hasScripts() {
203  return !scriptProcesses.isEmpty();
204  }
205 
206 }
int nextScriptId
The script ID for the next created script.
Interface for listeners interested in drawinfo messages received from the Crossfire server...
final Iterable< Spell > spellsManager
The spells manager instance to use.
final CommandQueue commandQueue
The CommandQueue for sending commands.
final ItemSet itemSet
The ItemSet instance to use.
final Stats stats
The Stats instance to watch.
void newScript(@NotNull final String command)
Creates a new script instance.
Implements the map model which is shown in the map and magic map views.
Definition: CfMap.java:22
void addScriptProcessListener(@NotNull final ScriptProcessListener scriptProcessListener)
Adds a ScriptProcessListener to be notified.the listener to add
void drawInfo(@NotNull String message, int color)
Pretends that a drawinfo message has been received.
boolean hasScripts()
Returns whether at least one script is running.
Maintains currently running script processes.
ScriptManager(@NotNull final CommandQueue commandQueue, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final Stats stats, @NotNull final FloorView floorView, @NotNull final ItemSet itemSet, @NotNull final Iterable< Spell > spellsManager, @NotNull final MapUpdaterState mapUpdaterState, @NotNull final SkillSet skillSet)
Creates a new instance.
Describes a Crossfire spell.
Definition: Spell.java:36
Set< ScriptProcess > getScriptByScriptId(final int scriptId)
Returns all running scripts matching a given script ID.
Set< ScriptProcess > getScripts(@NotNull final String partialScriptName)
Returns all running scripts matching a given (partial) name or a script ID.
Update a CfMap model from protocol commands.
final SkillSet skillSet
The SkillSet for looking up skill names.
final Collection< ScriptProcess > scriptProcesses
All running ScriptProcesses.
final CrossfireServerConnection crossfireServerConnection
The CrossfireServerConnection instance.
final FloorView floorView
The FloorView to use.
An external command executed as a client-sided script.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
Model class maintaining the CfItems known to the player.
Definition: ItemSet.java:43
Maintains the pending (ncom) commands sent to the server.
This is the representation of all the statistics of a player, like its speed or its experience...
Definition: Stats.java:43
Maintain the set of skills as sent by the server.
Definition: SkillSet.java:38
Set< ScriptProcess > getScriptsByName(@NotNull final CharSequence partialScriptName)
Returns all running scripts matching a given (partial) name.
final MapUpdaterState mapUpdaterState
The MapUpdaterState instance to use.
Provides a view to all items comprising the current floor view.
Definition: FloorView.java:35