Fix issue #82.

This commit is contained in:
Major-
2015-08-29 16:12:52 +01:00
parent 81a763cd69
commit c066742588
2 changed files with 32 additions and 15 deletions
@@ -21,6 +21,7 @@ import org.apollo.game.model.entity.EntityType;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import org.apollo.game.model.entity.obj.DynamicGameObject;
/** /**
* An 8x8 area of the map. * An 8x8 area of the map.
@@ -169,7 +170,8 @@ public final class Region {
*/ */
public Set<RegionUpdateMessage> encode(int height) { public Set<RegionUpdateMessage> encode(int height) {
Set<RegionUpdateMessage> additions = entities.values().stream() Set<RegionUpdateMessage> additions = entities.values().stream()
.flatMap(Set::stream).filter(entity -> entity instanceof GroupableEntity) .flatMap(Set::stream) // TODO fix this to work for ground items + projectiles
.filter(entity -> entity instanceof DynamicGameObject && entity.getPosition().getHeight() == height)
.map(entity -> ((GroupableEntity) entity).toUpdateOperation(this, EntityUpdateType.ADD).toMessage()) .map(entity -> ((GroupableEntity) entity).toUpdateOperation(this, EntityUpdateType.ADD).toMessage())
.collect(Collectors.toSet()); .collect(Collectors.toSet());
@@ -241,7 +243,11 @@ public final class Region {
* @return The Set of RegionUpdateMessages. * @return The Set of RegionUpdateMessages.
*/ */
public Set<RegionUpdateMessage> getUpdates(int height) { public Set<RegionUpdateMessage> getUpdates(int height) {
return ImmutableSet.copyOf(updates.get(height)); Set<RegionUpdateMessage> updates = this.updates.get(height);
Set<RegionUpdateMessage> copy = ImmutableSet.copyOf(updates);
updates.clear();
return copy;
} }
/** /**
@@ -309,20 +315,26 @@ public final class Region {
* Records the specified {@link GroupableEntity} as being updated this pulse. * Records the specified {@link GroupableEntity} as being updated this pulse.
* *
* @param entity The GroupableEntity. * @param entity The GroupableEntity.
* @param type The {@link EntityUpdateType}. * @param update The {@link EntityUpdateType}.
* @throws UnsupportedOperationException If the specified Entity cannot be operated on in this manner. * @throws UnsupportedOperationException If the specified Entity cannot be operated on in this manner.
*/ */
private <T extends Entity & GroupableEntity> void record(T entity, EntityUpdateType type) { private <T extends Entity & GroupableEntity> void record(T entity, EntityUpdateType update) {
UpdateOperation<?> operation = entity.toUpdateOperation(this, type); UpdateOperation<?> operation = entity.toUpdateOperation(this, update);
RegionUpdateMessage message = operation.toMessage(), inverse = operation.inverse(); RegionUpdateMessage message = operation.toMessage(), inverse = operation.inverse();
int height = entity.getPosition().getHeight(); int height = entity.getPosition().getHeight();
Set<RegionUpdateMessage> updates = this.updates.get(height); Set<RegionUpdateMessage> updates = this.updates.get(height);
if (entity.getEntityType() == EntityType.STATIC_OBJECT && type == EntityUpdateType.REMOVE) { EntityType type = entity.getEntityType();
if (type == EntityType.STATIC_OBJECT) {
if (update == EntityUpdateType.REMOVE) {
removedObjects.get(height).add(message); removedObjects.get(height).add(message);
} else { // TODO should this really be possible?
removedObjects.get(height).remove(inverse);
}
updates.add(message); updates.add(message);
updates.remove(inverse);
} else { } else {
updates.add(message); updates.add(message);
updates.remove(inverse); updates.remove(inverse);
@@ -2,6 +2,7 @@ package org.apollo.game.sync.task;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import org.apollo.game.message.impl.ClearRegionMessage; import org.apollo.game.message.impl.ClearRegionMessage;
@@ -140,20 +141,24 @@ public final class PrePlayerSynchronizationTask extends SynchronizationTask {
RegionRepository repository = player.getWorld().getRegionRepository(); RegionRepository repository = player.getWorld().getRegionRepository();
int height = position.getHeight(); int height = position.getHeight();
differences.stream().map(coordinates -> { for (RegionCoordinates coordinates : differences) {
Set<RegionUpdateMessage> messages = updates.computeIfAbsent(coordinates, Set<RegionUpdateMessage> messages = updates.computeIfAbsent(coordinates,
coords -> repository.get(coords).getUpdates(height)); coords -> repository.get(coords).getUpdates(height));
return new GroupedRegionUpdateMessage(position, coordinates, messages); if (!messages.isEmpty()) {
}).forEach(player::send); player.send(new GroupedRegionUpdateMessage(position, coordinates, messages));
}
}
full.stream().map(coordinates -> { for (RegionCoordinates coordinates : full) {
Set<RegionUpdateMessage> messages = encodes.computeIfAbsent(coordinates, Set<RegionUpdateMessage> messages = encodes.computeIfAbsent(coordinates,
coords -> repository.get(coords).encode(height)); coords -> repository.get(coords).encode(height));
if (!messages.isEmpty()) {
player.send(new ClearRegionMessage(position, coordinates)); player.send(new ClearRegionMessage(position, coordinates));
return new GroupedRegionUpdateMessage(position, coordinates, messages); player.send(new GroupedRegionUpdateMessage(position, coordinates, messages));
}).forEach(player::send); }
}
} }
} }