Reputation: 31
I'm a beginner in Java.
I have task to do multiplayer game. My main problem now is updating position of players. I write server and client application. I write class Players
when I store ArrayList
(PlayerData
) and this arraylist
is what I want to send via ObjectOutputStream
.
In console I see that all variables are ok but when server sends and client receives packets, all var like int ID
or String
are updated but arraylist
not.
I can't find solution, which I hope is very easy, but my bad - I can't find it. Please, Gods of Java, help me because I have been trying to fix it about 3 days.
public class Players {
private ArrayList<PlayerData> players = new ArrayList<PlayerData>();
public Players() {
// TODO Auto-generated constructor stub
}
public Players(ArrayList<PlayerData> listData) {
this.players = new ArrayList<PlayerData>();
players.addAll(listData);
System.err.println(players.get(0).getPosition().x + " " + players.get(0).getPosition().y);
}
public void add(int port,String username,Vector2 pos)
{
players.add(new PlayerData(port,username, pos));
}
public void update(int port, Vector2 move)
{
for (int i = 0 ; i < players.size() ; i++)
{
if ( players.get(i).getID() == port)
{
System.err.println("Poruszam sie " + " " + move.x + " " + move.y);
players.get(i).move(move);
}
}
}
public ArrayList<PlayerData> getPlayers()
{
return players;
}
public PlayerData getPlayer(int ID)
{
return players.get(ID);
}
public int size()
{
return players.size();
}
public void remove(int ID) {
// TODO Auto-generated method stub
if(ID <players.size())
players.remove(ID);
}
//server function handlemove
public void handleMove(Object obj,int port) {
// TODO Auto-generated method stub
PacketMove packet = (PacketMove)obj;
pos.x += packet.getPosition().x;
pos.y += packet.getPosition().y;
playersConnected.update(port, packet.getPosition());
System.err.println("Move " + playersConnected.getPlayer(0).getPosition().x + " " + playersConnected.getPlayer(0).getPosition().y);
//clients[getID(port)].send(new PacketMove(2, new Vector2(playersConnected.getPlayer(0).getPosition())));
// broadcast(new PacketMove(getID(port), new Vector2(playersConnected.getPlayer(port).getPosition())));
licznik +=1;
clients[0].send(new PacketUpdate(licznik, new Players(playersConnected.getPlayers())));
}
public class PlayerData implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4168716596929732412L;
private Vector2 position;
private String username;
private int ID = -1;
public PlayerData(int port,String username,Vector2 position) {
// TODO Auto-generated constructor stub
this.position = position;
this.username = username;
this.ID = port;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public Vector2 getPosition()
{
return position;
}
public void setPosition(Vector2 pos)
{
this.position = pos;
}
public int getID()
{
return ID;
}
public void move(Vector2 move) {
this.position.x += move.x;
this.position.y += move.y;
System.out.println(position.x + " " + position.y + " w playerdata");
}
}
Upvotes: 0
Views: 1095
Reputation: 637
You need to also list the what is the content of your send clients[0].send(new PacketUpdate(licznik, new Players(playersConnected.getPlayers())));
Also how you are sending and receiving on client and server.. only than we can exactly pin-point the issue.. Here I have listed a basic test that asserts array list of your PlayerData object is serialized. I have used junit4 -- so to run this you will need to add junit-4 jars in your classpath
based on your specific code, you can make modifications to this test to adjust..
import static org.junit.Assert.assertEquals;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
public class PlayersTest {
private List<PlayerData> list_of_object = new ArrayList<PlayerData>();
@Before
public void setUp() throws Exception {
for(int i=0; i< 10; i++){
PlayerData p = new PlayerData(i,"", new PlayersPosition(i, i*22));
list_of_object.add(p);
}
}
@Test
public void test() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(list_of_object);
byte[] dataToSend = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(dataToSend);
ObjectInputStream ois = new ObjectInputStream(bais);
List<PlayerData> deserialized = (List<PlayerData>) ois.readObject();
assertEquals(list_of_object.size(), deserialized.size());
for (int i=0; i < 10; i++){
assertEquals(list_of_object.get(i), deserialized.get(i));
}
}
public static class PlayersPosition implements Serializable {
private double x;
private double y;
public PlayersPosition(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PlayersPosition that = (PlayersPosition) o;
if (Double.compare(that.x, x) != 0) return false;
return Double.compare(that.y, y) == 0;
}
}
public static class PlayerData implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private PlayersPosition playersPosition;
private String username;
private int ID = -1;
public PlayerData(int port, String username, PlayersPosition playersPosition) {
// TODO Auto-generated constructor stub
this.playersPosition = playersPosition;
this.username = username;
this.ID = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public PlayersPosition getPlayersPosition() {
return playersPosition;
}
public void setPlayersPosition(PlayersPosition pos) {
this.playersPosition = pos;
}
public int getID() {
return ID;
}
public void move(PlayersPosition moveTo) {
this.playersPosition.x += moveTo.getX();
this.playersPosition.y += moveTo.getX();
System.out.println(playersPosition.getX() + " " + playersPosition.getX() + " w playerdata");
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PlayerData that = (PlayerData) o;
if (ID != that.ID) return false;
if (!playersPosition.equals(that.playersPosition)) return false;
return username.equals(that.username);
}
}
}
Upvotes: 1