Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

WIP - nullable location.

  • Loading branch information...
commit 709f632045a04823d81f1e1bc7802cbd14488a6f 1 parent accc4ce
@lhunath lhunath authored
View
5 omicron-api/src/main/java/com/lyndir/omicron/api/util/Maybe.java
@@ -16,6 +16,7 @@
package com.lyndir.omicron.api.util;
+import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import java.util.Objects;
import javax.annotation.Nonnull;
@@ -39,6 +40,10 @@
return reference == null? Maybe.<T>absent(): new Present<>( reference );
}
+ public static <T> Maybe<T> fromOptional(final Optional<T> reference) {
+ return reference.isPresent()? new Present<>( reference.get() ): Maybe.<T>absent();
+ }
+
public static <T> Maybe<T> of(@Nonnull final T reference) {
return new Present<>( reference );
}
View
20 omicron-core/src/main/java/com/lyndir/omicron/api/model/GameObject.java
@@ -32,6 +32,7 @@
private final int objectID;
private final ImmutableListMultimap<ModuleType<?>, Module> modules;
private Player owner;
+ @Nullable
private Tile location;
GameObject(@Nonnull final UnitType unitType, @Nonnull final Game game, @Nonnull final Player owner) {
@@ -121,17 +122,26 @@ public Game getGame() {
return game;
}
- Tile getLocation() {
- return location;
+ Optional<Tile> getLocation() {
+ return Optional.fromNullable( location );
}
@Override
public Maybe<Tile> checkLocation()
throws NotAuthenticatedException {
- if (!isGod() && !currentPlayer().canObserve( getLocation() ).isTrue())
- return Maybe.unknown();
+ Optional<Tile> location = getLocation();
+ if (!isGod()) {
+ if (location.isPresent()) {
+ if (!currentPlayer().canObserve( location.get() ).isTrue())
+ // Has a location but current player cannot observe it.
+ return Maybe.unknown();
+ } else if (!isOwnedByCurrentPlayer())
+ // Has no location and not owned by current player.
+ return Maybe.unknown();
+ }
- return Maybe.fromNullable( getLocation() );
+ // We're either god, can be observed by or are owned by the current player.
+ return Maybe.fromOptional( location );
}
void setLocation(@Nonnull final Tile location) {
Please sign in to comment.
Something went wrong with that request. Please try again.