Gridarta Editor
AtrinikServerActions.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.var.atrinik.actions;
21 
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.net.Socket;
31 import org.jetbrains.annotations.NotNull;
32 
38 public class AtrinikServerActions extends AbstractServerActions<GameObject, MapArchObject, Archetype> {
39 
43  private static final int SERVER_CMD_CONTROL = 0;
44 
48  @NotNull
49  private static final String APPLICATION_NAME_IDENTIFIER = "gridarta";
50 
54  @NotNull
55  private static final String CHARSET_NAME = "US-ASCII";
56 
60  private static final int CMD_CONTROL_MAP = 1;
61 
65  private static final int CMD_CONTROL_PLAYER = 2;
66 
70  private static final int CMD_CONTROL_MAP_RESET = 1;
71 
75  private static final int CMD_CONTROL_PLAYER_TELEPORT = 1;
76 
82  super(fileControl);
83  }
84 
90  private void sendPacket(@NotNull final ByteArrayOutputStream stream) throws IOException {
91  final byte[] packet = stream.toByteArray();
92  packet[0] = (byte) ((packet.length - 2) >> 8);
93  packet[1] = (byte) (packet.length - 2);
94  try {
95  try (Socket socket = new Socket("127.0.0.1", 13327)) {
96  final OutputStream outputStream = socket.getOutputStream();
97  outputStream.write(packet);
98  socket.shutdownOutput();
99  }
100  } catch (final IOException ex) {
101  throw new IOException("127.0.0.1:13327: " + ex.getMessage(), ex);
102  }
103  }
104 
105  @Override
106  protected void resetMap(@NotNull final String mapPath) throws IOException {
107  final ByteArrayOutputStream tmp = new ByteArrayOutputStream();
108  tmp.write(0);
109  tmp.write(0);
110  tmp.write(SERVER_CMD_CONTROL);
111  tmp.write(APPLICATION_NAME_IDENTIFIER.getBytes(CHARSET_NAME));
112  tmp.write(0); // termination of application name identifier
113  tmp.write(CMD_CONTROL_MAP);
114  tmp.write(CMD_CONTROL_MAP_RESET);
115  tmp.write(mapPath.getBytes(CHARSET_NAME));
116  tmp.write(0);
117  sendPacket(tmp);
118  }
119 
120  @Override
121  protected void teleportCharacterToMap(@NotNull final String mapPath, final int mapX, final int mapY) throws IOException {
122  final ByteArrayOutputStream tmp = new ByteArrayOutputStream();
123  tmp.write(0);
124  tmp.write(0);
125  tmp.write(SERVER_CMD_CONTROL);
126  tmp.write(APPLICATION_NAME_IDENTIFIER.getBytes(CHARSET_NAME));
127  tmp.write(0); // termination of application name identifier
128  tmp.write(CMD_CONTROL_PLAYER);
129  tmp.write(CMD_CONTROL_PLAYER_TELEPORT);
130  tmp.write(0); // empty player name
131  tmp.write(mapPath.getBytes(CHARSET_NAME));
132  tmp.write(0);
133  tmp.write(mapX >> 8);
134  tmp.write(mapX);
135  tmp.write(mapY >> 8);
136  tmp.write(mapY);
137  sendPacket(tmp);
138  }
139 
140 }
net.sf.gridarta.model.mapmanager
Definition: AbstractMapManager.java:20
net.sf.gridarta.actions.AbstractServerActions
Definition: AbstractServerActions.java:44
net.sf.gridarta.var.atrinik.actions.AtrinikServerActions.APPLICATION_NAME_IDENTIFIER
static final String APPLICATION_NAME_IDENTIFIER
Definition: AtrinikServerActions.java:49
net.sf.gridarta
net.sf
net.sf.gridarta.model.mapmanager.FileControl
Definition: FileControl.java:30
net.sf.gridarta.var.atrinik.actions.AtrinikServerActions.resetMap
void resetMap(@NotNull final String mapPath)
Definition: AtrinikServerActions.java:106
net.sf.gridarta.var
net.sf.gridarta.var.atrinik.model.archetype.Archetype
Definition: Archetype.java:30
net.sf.gridarta.var.atrinik.actions.AtrinikServerActions
Definition: AtrinikServerActions.java:38
net
net.sf.gridarta.var.atrinik.actions.AtrinikServerActions.SERVER_CMD_CONTROL
static final int SERVER_CMD_CONTROL
Definition: AtrinikServerActions.java:43
net.sf.gridarta.var.atrinik.actions.AtrinikServerActions.CMD_CONTROL_PLAYER_TELEPORT
static final int CMD_CONTROL_PLAYER_TELEPORT
Definition: AtrinikServerActions.java:75
net.sf.gridarta.var.atrinik.model.maparchobject.MapArchObject
Definition: MapArchObject.java:39
net.sf.gridarta.var.atrinik.actions.AtrinikServerActions.sendPacket
void sendPacket(@NotNull final ByteArrayOutputStream stream)
Definition: AtrinikServerActions.java:90
net.sf.gridarta.var.atrinik
net.sf.gridarta.var.atrinik.model.gameobject.GameObject
Definition: GameObject.java:44
net.sf.gridarta.var.atrinik.actions.AtrinikServerActions.CMD_CONTROL_MAP
static final int CMD_CONTROL_MAP
Definition: AtrinikServerActions.java:60
net.sf.gridarta.var.atrinik.actions.AtrinikServerActions.teleportCharacterToMap
void teleportCharacterToMap(@NotNull final String mapPath, final int mapX, final int mapY)
Definition: AtrinikServerActions.java:121
net.sf.gridarta.var.atrinik.model.maparchobject
Definition: DefaultMapArchObjectFactory.java:20
net.sf.gridarta.model
net.sf.gridarta.var.atrinik.model
net.sf.gridarta.actions
Definition: AbstractServerActions.java:20
net.sf.gridarta.var.atrinik.actions.AtrinikServerActions.CMD_CONTROL_PLAYER
static final int CMD_CONTROL_PLAYER
Definition: AtrinikServerActions.java:65
net.sf.gridarta.var.atrinik.actions.AtrinikServerActions.CHARSET_NAME
static final String CHARSET_NAME
Definition: AtrinikServerActions.java:55
net.sf.gridarta.var.atrinik.model.gameobject
Definition: DefaultGameObjectFactory.java:20
net.sf.gridarta.var.atrinik.actions.AtrinikServerActions.AtrinikServerActions
AtrinikServerActions(@NotNull final FileControl< GameObject, MapArchObject, Archetype > fileControl)
Definition: AtrinikServerActions.java:81
net.sf.gridarta.actions.AbstractServerActions.fileControl
final FileControl< G, A, R > fileControl
Definition: AbstractServerActions.java:56
net.sf.gridarta.var.atrinik.model.archetype
Definition: Archetype.java:20
net.sf.gridarta.var.atrinik.actions.AtrinikServerActions.CMD_CONTROL_MAP_RESET
static final int CMD_CONTROL_MAP_RESET
Definition: AtrinikServerActions.java:70