22 package com.realtime.crossfire.jxclient.window;
29 import java.io.BufferedReader;
30 import java.io.BufferedWriter;
32 import java.io.FileInputStream;
33 import java.io.FileNotFoundException;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStreamReader;
37 import java.io.OutputStreamWriter;
38 import org.jetbrains.annotations.NotNull;
58 public static void loadShortcuts(@NotNull
final Shortcuts shortcuts, @NotNull
final CharSequence hostname, @NotNull
final CharSequence character) {
62 }
catch (
final IOException ex) {
63 System.err.println(
"Cannot read shortcuts file: "+ex.getMessage());
68 shortcuts.clearShortcuts();
70 try (
final FileInputStream fis =
new FileInputStream(file)) {
71 try (
final InputStreamReader isr =
new InputStreamReader(fis,
"UTF-8")) {
72 try (
final BufferedReader br =
new BufferedReader(isr)) {
77 }
catch (
final IOException ex) {
78 shortcuts.clearShortcuts();
79 shortcuts.resetModified();
80 shortcuts.setFile(file);
83 shortcuts.resetModified();
84 shortcuts.setFile(file);
85 }
catch (
final FileNotFoundException ignored) {
88 }
catch (
final IOException ex) {
89 System.err.println(
"Cannot read shortcuts file "+file+
": "+ex.getMessage());
101 private static void loadShortcuts(@NotNull
final Shortcuts shortcuts, @NotNull
final BufferedReader br)
throws IOException {
104 final String line = br.readLine();
109 if (line.equals(
"empty")) {
110 shortcuts.setShortcut(index, null);
112 }
else if (line.startsWith(
"spell cast ")) {
113 shortcuts.setSpellShortcut(index, line.substring(11).trim(),
true);
115 }
else if (line.startsWith(
"spell invoke ")) {
116 shortcuts.setSpellShortcut(index, line.substring(13).trim(),
false);
118 }
else if (line.startsWith(
"command ")) {
119 shortcuts.setCommandShortcut(index, line.substring(8).trim());
122 System.err.println(
"shortcut: ignoring undefined entry '"+line+
"'");
133 if (!shortcuts.isModified()) {
137 final File file = shortcuts.getFile();
142 try (
final FileOutputStream fos =
new FileOutputStream(file)) {
143 try (
final OutputStreamWriter osw =
new OutputStreamWriter(fos)) {
144 try (
final BufferedWriter bw =
new BufferedWriter(osw)) {
145 for (
final Shortcut shortcut : shortcuts) {
146 if (shortcut == null) {
149 final ShortcutSpell shortcutSpell = (ShortcutSpell)shortcut;
151 bw.write(shortcutSpell.
isCast() ?
"cast " :
"invoke ");
155 final ShortcutCommand shortcutCommand = (ShortcutCommand)shortcut;
156 bw.write(
"command ");
160 throw new AssertionError();
166 }
catch (
final IOException ex) {
167 System.err.println(
"Cannot write shortcuts file "+shortcuts.getFile()+
": "+ex.getMessage());
171 shortcuts.resetModified();
String getName()
Returns the spell name.
Utility class to return references to settings files.
static File getShortcutsFile(@NotNull final CharSequence hostname, @NotNull final CharSequence character)
Returns the shortcuts file.
static void loadShortcuts(@NotNull final Shortcuts shortcuts, @NotNull final CharSequence hostname, @NotNull final CharSequence character)
Load shortcut info from the backing file.
static void saveShortcuts(@NotNull final Shortcuts shortcuts)
Save all shortcut info to the backing file.
ShortcutsLoader()
Private constructor to prevent instantiation.
String getCommand()
Returns the command to execute.
boolean isCast()
Returns whether the spell should be "cast" or "invoked".
static void loadShortcuts(@NotNull final Shortcuts shortcuts, @NotNull final BufferedReader br)
Load shortcut info.
Spell getSpell()
Returns the spell to cast.
Abstract base class for shortcut commands.
A Shortcut that casts a spell.
A Shortcut that executes a Crossfire command.
Manages a list of Shortcuts.