Skip to content

Commit

Permalink
Fix the client
Browse files Browse the repository at this point in the history
  • Loading branch information
kitskub committed Sep 14, 2014
1 parent 7cfe9e8 commit 011d9ab
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ protected void updateObserver() {
observingChunks.set(observing);
}

public Set<ChunkReference> getObservingChunks() {
return observingChunks.get();
}

public abstract void copySnapshot();

protected abstract void startObserving(ImmutableSet<ChunkReference> observing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,52 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.flowpowered.api.component;
package com.flowpowered.api.component.entity;

import java.util.Set;

import com.flowpowered.api.component.Component;
import com.flowpowered.api.entity.Entity;
import com.flowpowered.api.event.EntityStartObservingChunksEvent;
import com.flowpowered.api.event.EntityStopObservingChunksEvent;
import com.flowpowered.api.geo.reference.ChunkReference;
import com.flowpowered.api.player.Player;
import com.flowpowered.events.EventHandler;

public class PlayerObserveChunksComponent extends Component {
private Player controller;

@Override
public void onTick(float dt) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean canTick() {
throw new UnsupportedOperationException("Not supported yet.");
return false;
}

public void setController(Player player) {
Set<ChunkReference> chunks = ((Entity) getOwner()).getObserver().getObservingChunks();
if (this.controller != null) {
controller.getNetwork().removeChunks(chunks);
}
this.controller = player;
if (this.controller != null) {
controller.getNetwork().addChunks(chunks);
}
}

@EventHandler
public void onObserve(EntityStartObservingChunksEvent e) {
if (e.getObserver().equals(getOwner()) && controller != null) {
controller.getNetwork().addChunks(e.getObserved());
}
}

@EventHandler
public void onStopObserve(EntityStopObservingChunksEvent e) {
if (e.getObserver().equals(getOwner()) && controller != null) {
controller.getNetwork().removeChunks(e.getObserved());
}
}
}
24 changes: 12 additions & 12 deletions src/main/java/com/flowpowered/api/player/PlayerNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import com.flowpowered.api.Client;
import com.flowpowered.api.geo.LoadOption;
Expand Down Expand Up @@ -58,8 +58,8 @@ public class PlayerNetwork {
* Chunks that have been sent to the client
*/
private final Set<ChunkReference> activeChunks = new LinkedHashSet<>();
private final Set<ChunkReference> chunkSendQueue = new LinkedHashSet<>();
private final Set<ChunkReference> chunkFreeQueue = new LinkedHashSet<>();
private final ConcurrentLinkedQueue<ChunkReference> chunkSendQueue = new ConcurrentLinkedQueue<>();
private final ConcurrentLinkedQueue<ChunkReference> chunkFreeQueue = new ConcurrentLinkedQueue<>();

protected volatile Transform previousTransform = Transform.INVALID;
private final AtomicReference<RepositionManager> rm = new AtomicReference<>(NullRepositionManager.INSTANCE);
Expand Down Expand Up @@ -92,17 +92,15 @@ public void setRepositionManager(RepositionManager rm) {
}
}

public void addChunks(Set<Chunk> chunks) {
chunkSendQueue.addAll(chunks.stream().map(ChunkReference::new).collect(
Collectors.toList()));
public void addChunks(Set<ChunkReference> chunks) {
chunkSendQueue.addAll(chunks);
}

public void removeChunks(Set<Chunk> toRemove) {
for (Chunk chunk : toRemove) {
ChunkReference ref = new ChunkReference(chunk);
public void removeChunks(Set<ChunkReference> toRemove) {
toRemove.stream().forEach((ref) -> {
chunkFreeQueue.add(ref);
chunks.remove(ref);
}
});
}

public void preSnapshotRun(Transform transform) {
Expand All @@ -117,6 +115,9 @@ public void preSnapshotRun(Transform transform) {
// We will sync old chunks, but not new ones
Set<ChunkReference> toSync = new LinkedHashSet<>(activeChunks);

sendPositionUpdates(transform);
previousTransform = transform;

// Now send new chunks
int chunksSentThisTick = 0;

Expand All @@ -135,8 +136,7 @@ public void preSnapshotRun(Transform transform) {
previousTransform = transform;

// Update the active chunks
for (Iterator<ChunkReference> it = toSync.iterator(); it.hasNext();) {
ChunkReference ref = it.next();
for (ChunkReference ref : toSync) {
Chunk chunk = ref.get();
// If it was unloaded, we have to free it
// We don't remove it from our chunks though
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/flowpowered/engine/FlowClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void onAdd() {
MaterialRegistry.setupClient();
}
// TEST CODE
FlowServerWorld world = new FlowServerWorld(engine, "TestWorld", null);
FlowServerWorld world = new FlowServerWorld(engine, "fallback", null);
engine.getWorldManager().addWorld(world);
world.getThread().start();
client.connect(new InetSocketAddress(engine.getArgs().server, engine.getArgs().port));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.atomic.AtomicReference;

import com.flowpowered.engine.FlowEngine;
import com.flowpowered.engine.player.FlowPlayer;
import com.flowpowered.networking.NetworkClient;
import com.flowpowered.networking.session.Session;

Expand All @@ -44,6 +45,7 @@ public Session newSession(Channel c) {
if (!session.compareAndSet(null, new FlowSession(engine, c))) {
throw new IllegalStateException("Two sessions created on the client!");
}
session.get().setPlayer(new FlowPlayer(session.get(), "Flowy"));
return session.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ChunkDataHandler extends FlowMessageHandler<ChunkDataMessage> {
public void handleClient(FlowSession session, ChunkDataMessage message) {
FlowClient client = session.getEngine().get(FlowClient.class);
// TODO: allow adding chunks to other worlds
FlowWorld world = (FlowWorld) client.getSession().getPlayer().getTransformProvider().getTransform().getPosition().getWorld().get();
FlowWorld world = (FlowWorld) client.getTransform().getPosition().getWorld().refresh(session.getEngine().getWorldManager());
if (message.isUnload()) {
world.setChunk(message.getX(), message.getY(), message.getZ(), null);
} else {
Expand Down

0 comments on commit 011d9ab

Please sign in to comment.