Search in sources :

Example 1 with SerializedEntityFile

use of io.xol.chunkstories.entity.SerializedEntityFile in project chunkstories by Hugobros3.

the class Ingame method destroy.

@Override
public void destroy() {
    // Logout sequence: Save the player entity
    if (world instanceof WorldMaster) {
        Player player = getPlayer();
        PlayerLogoutEvent playerDisconnectionEvent = new PlayerLogoutEvent(player);
        world.getPluginManager().fireEvent(playerDisconnectionEvent);
        if (this.playerEntity != null) {
            SerializedEntityFile playerEntityFile = new SerializedEntityFile(world.getFolderPath() + "/players/" + getPlayer().getName().toLowerCase() + ".csf");
            playerEntityFile.write(this.playerEntity);
        }
    }
    // Stop the game logic and save
    if (world instanceof WorldMaster) {
        // TODO: Stop simulation
        Fence fence = ((WorldMaster) world).stopLogic();
        // exitButton.text = "#{world.saving}";
        fence.traverse();
        fence = world.saveEverything();
        // exitButton.text = "#{world.saving}";
        fence.traverse();
    }
    // Disables plugins
    world.getPluginManager().disablePlugins();
    this.world.getWorldRenderer().destroy();
}
Also used : Player(io.xol.chunkstories.api.player.Player) LocalPlayer(io.xol.chunkstories.api.client.LocalPlayer) PlayerLogoutEvent(io.xol.chunkstories.api.events.player.PlayerLogoutEvent) SerializedEntityFile(io.xol.chunkstories.entity.SerializedEntityFile) Fence(io.xol.chunkstories.api.util.concurrency.Fence) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Example 2 with SerializedEntityFile

use of io.xol.chunkstories.entity.SerializedEntityFile in project chunkstories by Hugobros3.

the class ServerPlayer method save.

/**
 * Serializes the stuff describing this player
 */
public void save() {
    long lastTime = playerDataFile.getLong("timeplayed", 0);
    long lastLogin = playerDataFile.getLong("lastlogin", 0);
    if (controlledEntity != null) {
        // Useless, kept for admin easyness, scripts, whatnot
        Location controlledEntityLocation = controlledEntity.getLocation();
        // Safely assumes as a SERVER the world will be master ;)
        WorldMaster world = (WorldMaster) controlledEntityLocation.getWorld();
        playerDataFile.setDouble("posX", controlledEntityLocation.x());
        playerDataFile.setDouble("posY", controlledEntityLocation.y());
        playerDataFile.setDouble("posZ", controlledEntityLocation.z());
        playerDataFile.setString("world", world.getWorldInfo().getInternalName());
        // Serializes the whole player entity !!!
        SerializedEntityFile playerEntityFile = new SerializedEntityFile(world.getFolderPath() + "/players/" + this.getName().toLowerCase() + ".csf");
        playerEntityFile.write(controlledEntity);
    }
    // Telemetry (EVIL)
    playerDataFile.setString("timeplayed", "" + (lastTime + (System.currentTimeMillis() - lastLogin)));
    playerDataFile.save();
    System.out.println("Player profile " + name + " saved.");
}
Also used : SerializedEntityFile(io.xol.chunkstories.entity.SerializedEntityFile) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) Location(io.xol.chunkstories.api.Location)

Example 3 with SerializedEntityFile

use of io.xol.chunkstories.entity.SerializedEntityFile in project chunkstories by Hugobros3.

the class WorldImplementation method spawnPlayer.

public void spawnPlayer(Player player) {
    if (!(this instanceof WorldMaster))
        throw new UnsupportedOperationException("Only Master Worlds can do this");
    Entity savedEntity = null;
    SerializedEntityFile playerEntityFile = new SerializedEntityFile(this.getFolderPath() + "/players/" + player.getName().toLowerCase() + ".csf");
    if (playerEntityFile.exists())
        savedEntity = playerEntityFile.read(this);
    Location previousLocation = null;
    if (savedEntity != null)
        previousLocation = savedEntity.getLocation();
    PlayerSpawnEvent playerSpawnEvent = new PlayerSpawnEvent(player, (WorldMaster) this, savedEntity, previousLocation);
    getGameContext().getPluginManager().fireEvent(playerSpawnEvent);
    if (!playerSpawnEvent.isCancelled()) {
        Entity entity = playerSpawnEvent.getEntity();
        Location actualSpawnLocation = playerSpawnEvent.getSpawnLocation();
        if (actualSpawnLocation == null)
            actualSpawnLocation = this.getDefaultSpawnLocation();
        // TODO EntitySimplePlayer ?
        if (entity == null || ((entity instanceof EntityLiving) && (((EntityLiving) entity).isDead())))
            entity = this.gameContext.getContent().entities().getEntityDefinition("player").create(actualSpawnLocation);
        else
            // entity = new EntityPlayer(this, 0d, 0d, 0d, player.getName()); //Default entity
            entity.setUUID(-1);
        // Name your player !
        if (entity instanceof EntityNameable)
            ((EntityNameable) entity).getNameComponent().setName(player.getName());
        entity.setLocation(actualSpawnLocation);
        addEntity(entity);
        if (entity instanceof EntityControllable)
            player.setControlledEntity((EntityControllable) entity);
        else
            System.out.println("Error : entity is not controllable");
    }
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) EntityLiving(io.xol.chunkstories.api.entity.EntityLiving) SerializedEntityFile(io.xol.chunkstories.entity.SerializedEntityFile) PlayerSpawnEvent(io.xol.chunkstories.api.events.player.PlayerSpawnEvent) EntityNameable(io.xol.chunkstories.api.entity.interfaces.EntityNameable) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) Location(io.xol.chunkstories.api.Location)

Aggregations

WorldMaster (io.xol.chunkstories.api.world.WorldMaster)3 SerializedEntityFile (io.xol.chunkstories.entity.SerializedEntityFile)3 Location (io.xol.chunkstories.api.Location)2 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)1 Entity (io.xol.chunkstories.api.entity.Entity)1 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)1 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)1 EntityNameable (io.xol.chunkstories.api.entity.interfaces.EntityNameable)1 PlayerLogoutEvent (io.xol.chunkstories.api.events.player.PlayerLogoutEvent)1 PlayerSpawnEvent (io.xol.chunkstories.api.events.player.PlayerSpawnEvent)1 Player (io.xol.chunkstories.api.player.Player)1 Fence (io.xol.chunkstories.api.util.concurrency.Fence)1