mirror of
				https://github.com/simon987/Much-Assembly-Required.git
				synced 2025-11-04 01:56:53 +00:00 
			
		
		
		
	JSONSerialisable objects now have a debug function #156
This commit is contained in:
		
							parent
							
								
									92008e553a
								
							
						
					
					
						commit
						78f98c8227
					
				@ -75,6 +75,15 @@ public class HarvesterNPC extends NonPlayerCharacter {
 | 
			
		||||
        return json;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public JSONObject debugJsonSerialise() {
 | 
			
		||||
        JSONObject json = jsonSerialise();
 | 
			
		||||
 | 
			
		||||
        json.put("taskCompleted", getTask().checkCompleted());
 | 
			
		||||
 | 
			
		||||
        return json;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Document mongoSerialise() {
 | 
			
		||||
        Document dbObject = super.mongoSerialise();
 | 
			
		||||
 | 
			
		||||
@ -5,6 +5,8 @@ import net.simon987.server.game.objects.MessageReceiver;
 | 
			
		||||
import net.simon987.server.game.objects.Structure;
 | 
			
		||||
import net.simon987.server.game.objects.Updatable;
 | 
			
		||||
import org.bson.Document;
 | 
			
		||||
import org.json.simple.JSONArray;
 | 
			
		||||
import org.json.simple.JSONObject;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
@ -61,4 +63,18 @@ public class RadioTower extends Structure implements MessageReceiver, Updatable
 | 
			
		||||
        return lastMessages;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public JSONObject debugJsonSerialise() {
 | 
			
		||||
        JSONObject json = super.debugJsonSerialise();
 | 
			
		||||
 | 
			
		||||
        JSONArray messages = new JSONArray();
 | 
			
		||||
 | 
			
		||||
        for (char[] message : this.messages) {
 | 
			
		||||
            messages.add(new String(message));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        json.put("messages", messages);
 | 
			
		||||
 | 
			
		||||
        return json;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -42,6 +42,11 @@ public abstract class Item implements JSONSerialisable, MongoSerializable {
 | 
			
		||||
        return json;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public JSONObject debugJsonSerialise() {
 | 
			
		||||
        return jsonSerialise();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Document mongoSerialise() {
 | 
			
		||||
        Document document = new Document();
 | 
			
		||||
 | 
			
		||||
@ -234,6 +234,11 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
 | 
			
		||||
        return json;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public JSONObject debugJsonSerialise() {
 | 
			
		||||
        return jsonSerialise();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public boolean isAt(int x, int y) {
 | 
			
		||||
        return this.x == x && this.y == y;
 | 
			
		||||
 | 
			
		||||
@ -139,6 +139,11 @@ public class TileMap implements JSONSerialisable, MongoSerializable {
 | 
			
		||||
        return json;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public JSONObject debugJsonSerialise() {
 | 
			
		||||
        return jsonSerialise();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Document mongoSerialise() {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -6,4 +6,6 @@ public interface JSONSerialisable {
 | 
			
		||||
 | 
			
		||||
    JSONObject jsonSerialise();
 | 
			
		||||
 | 
			
		||||
    JSONObject debugJsonSerialise();
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -15,14 +15,14 @@ public class ObjectsRequestHandler implements MessageHandler {
 | 
			
		||||
    @Override
 | 
			
		||||
    public void handle(OnlineUser user, JSONObject json) throws IllegalStateException, IOException {
 | 
			
		||||
        if (json.get("t").equals("object")) {
 | 
			
		||||
            // LogManager.LOGGER.fine("(WS) Objects request from " + user.getUser().getUsername());
 | 
			
		||||
 | 
			
		||||
            int x, y;
 | 
			
		||||
            String dimension;
 | 
			
		||||
            boolean sendDebugInfo;
 | 
			
		||||
            try {
 | 
			
		||||
                x = Long.valueOf((long) json.get("x")).intValue();
 | 
			
		||||
                y = Long.valueOf((long) json.get("y")).intValue();
 | 
			
		||||
                dimension = (String) json.get("dimension");
 | 
			
		||||
                sendDebugInfo = json.containsKey("debug") && (boolean) json.get("debug");
 | 
			
		||||
            } catch (Exception e) {
 | 
			
		||||
                LogManager.LOGGER.severe("(WS) Malformed Objects request from " + user.getUser().getUsername());
 | 
			
		||||
                return;
 | 
			
		||||
@ -37,7 +37,11 @@ public class ObjectsRequestHandler implements MessageHandler {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                for (GameObject object : world.getGameObjects()) {
 | 
			
		||||
                    objects.add(object.jsonSerialise());
 | 
			
		||||
                    if (sendDebugInfo) {
 | 
			
		||||
                        objects.add(object.debugJsonSerialise());
 | 
			
		||||
                    } else {
 | 
			
		||||
                        objects.add(object.jsonSerialise());
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                response.put("t", "object");
 | 
			
		||||
 | 
			
		||||
@ -105,6 +105,7 @@ public class SocketServer {
 | 
			
		||||
     */
 | 
			
		||||
    public void tick() {
 | 
			
		||||
 | 
			
		||||
        //TODO: refactor this function (1. Create json instance for each user 2. Extract functions 3. rename variables)
 | 
			
		||||
        JSONObject json = new JSONObject();
 | 
			
		||||
        json.put("t", "tick");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user