Skip to content

Commit

Permalink
Lots of cleanup, including: Make full use of new Java 8 features, rem…
Browse files Browse the repository at this point in the history
…ove redundant casts, improve & fix JavaDocs, remove unused imports and more
  • Loading branch information
olliestanley committed Sep 14, 2014
1 parent 0de7e37 commit 7cfe9e8
Show file tree
Hide file tree
Showing 51 changed files with 103 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Iterator<Vector3i> getIteratorFor(int centerX, int centerY, int centerZ)
/**
* Chunks currently being observed
*/
protected final AtomicReference<Set<ChunkReference>> observingChunks = new AtomicReference<Set<ChunkReference>>(new HashSet<ChunkReference>());
protected final AtomicReference<Set<ChunkReference>> observingChunks = new AtomicReference<>(new HashSet<>());
protected final AtomicReference<ChunkIterator> liveObserverIterator = new AtomicReference<>(NO_CHUNKS);
protected final Engine engine;
protected boolean observeChunksFailed = true;
Expand All @@ -70,7 +70,7 @@ public class SyncDistanceChunkIterator implements ChunkIterator {
public Iterator<Vector3i> getIteratorFor(int centerX, int centerY, int centerZ) {
return new OutwardIterator(centerX, centerY, centerZ, getSyncDistance());
}
};
}

/**
* Returns if the owning {@link com.flowpowered.api.entity.Entity} is an observer. <p/> Observer means the Entity can trigger network updates (such as chunk creation) within its sync distance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public <T extends Component> Collection<T> getAll(Class<T> type) {

@SuppressWarnings ("unchecked")
@Override
public <T extends Object> Collection<T> getAllOfType(Class<T> type) {
public <T> Collection<T> getAllOfType(Class<T> type) {
Preconditions.checkNotNull(type);
synchronized (components) {
ArrayList<T> matches = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package com.flowpowered.api.generator;

import com.flowpowered.api.geo.World;
import com.flowpowered.api.geo.cuboid.Chunk;
import com.flowpowered.api.material.BlockMaterial;
import com.flowpowered.api.util.cuboid.CuboidBlockMaterialBuffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
package com.flowpowered.api.generator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.flowpowered.api.geo.World;
import com.flowpowered.api.geo.cuboid.Chunk;
import com.flowpowered.api.material.BlockMaterial;
import com.flowpowered.api.util.cuboid.CuboidBlockMaterialBuffer;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/flowpowered/api/geo/AreaBlockAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public interface AreaBlockAccess extends AreaBlockSource {
* @param cause of the change, or null if non-specific cause
* @return the old data for the block
*/
public short clearBlockDataBits(int x, int y, int z, int bits, Cause<?> source);
public short clearBlockDataBits(int x, int y, int z, int bits, Cause<?> cause);

/**
* Gets the data field from the block at (x, y, z)<br> <br> field = (data & bits) >> (shift)<br> <br> The shift value used shifts the least significant non-zero bit of bits to the LSB position
Expand Down Expand Up @@ -136,7 +136,7 @@ public interface AreaBlockAccess extends AreaBlockSource {
* @param cause of the change, or null if non-specific cause
* @return the old value of the field
*/
public int setBlockDataField(int x, int y, int z, int bits, int value, Cause<?> source);
public int setBlockDataField(int x, int y, int z, int bits, int value, Cause<?> cause);

/**
* Adds a value to the data field for the block at (x, y, z). This is the reverse operation to the getBlockDataField method.<br> <br> newData = (((oldData + (value << shift)) & bits) | (oldData &
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void timeoutCheck() {
if (baa != null) {
try {
baa.closeIfTimedOut();
} catch (IOException ioe) {
} catch (IOException ignore) {
}
if (baa.isClosed()) {
baaRef.compareAndSet(baa, null);
Expand All @@ -73,7 +73,7 @@ public boolean attemptClose() {
baaRef.compareAndSet(baa, null);
}
return success;
} catch (IOException ioe) {
} catch (IOException ignore) {
}
}
return true;
Expand All @@ -96,8 +96,7 @@ public boolean inputStreamExists(int i) {
}
try {
return baa.exists(i);
} catch (BAAClosedException e) {
continue;
} catch (BAAClosedException ignore) {
} catch (IOException e) {
return false;
}
Expand All @@ -122,8 +121,7 @@ public boolean delete(int i) {
try {
baa.delete(i);
return true;
} catch (BAAClosedException e) {
continue;
} catch (BAAClosedException ignore) {
} catch (IOException e) {
return false;
}
Expand Down Expand Up @@ -218,7 +216,7 @@ private ByteArrayArray getByteArrayArray() {
if (count > 10) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
} catch (InterruptedException ignore) {
}
} else if (count > 0) {
Thread.yield();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ public long length() throws IOException {
}

public void close() throws IOException {
for (MappedByteBuffer m : pages) {
if (m != null) {
m.force();
}
}
pages.stream().filter(m -> m != null).forEach(MappedByteBuffer::force);
lock.release();
file.close();
}
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/flowpowered/api/material/BlockMaterial.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ public boolean hasPhysics() {
*
* @param oldMaterial the previous material, or null if the update was not due to a material change
* @param block that got updated
* @return true if the block was updated
*/
public void onUpdate(BlockMaterial oldMaterial, Block block) {
}
Expand Down Expand Up @@ -308,7 +307,6 @@ public ByteBitSet getOcclusion() {
/**
* Sets the occludes faces of this Block Material<br> Occluded faces do not let light though and require rendering behind it at those faces
*
* @param data of this Block Material
* @param faces to make this Block Material occlude
* @return this Block Material
*/
Expand Down Expand Up @@ -349,7 +347,7 @@ public void onPlacement(Block block, short data, BlockFace against, Vector3f cli
}

/**
* Checks the block to see if it can be created at that position<br> Orientation-specific checks are performed in the {@link #canPlace(com.flowpowered.api.geo.cuboid.Block, short, com.flowpowered.api.material.block.BlockFace, com.flowpowered.math.vector.Vector3f, boolean, com.flowpowered.api.event.Cause)} method<br> Use this method to see if creation is possible at a
* Checks the block to see if it can be created at that position<br> Orientation-specific checks are performed in the {@link #canPlace(Block, short, BlockFace, Vector3f, boolean, Cause)} method<br> Use this method to see if creation is possible at a
* given position when not placed
*
* @param block this Block Material should be created in
Expand All @@ -362,7 +360,7 @@ public boolean canCreate(Block block, short data, Cause<?> cause) {
}

/**
* Creates this Block Material at a block in the world<br> Orientation-specific changes are performed in the {@link #onPlacement(com.flowpowered.api.geo.cuboid.Block, short, com.flowpowered.api.material.block.BlockFace, com.flowpowered.math.vector.Vector3f, boolean, com.flowpowered.api.event.Cause)} method<br> Use this method to create the block at a given position
* Creates this Block Material at a block in the world<br> Orientation-specific changes are performed in the {@link #onPlacement(Block, short, BlockFace, Vector3f, boolean, Cause)} method<br> Use this method to create the block at a given position
* when not placed
*
* @param block to create this Block Material in
Expand Down Expand Up @@ -411,9 +409,9 @@ public boolean isCompatibleWith(BlockMaterial m) {
}

/**
* Helper method to create a MaterialCause.
* Helper method to create a Cause of generic type BlockMaterial.
*
* Same as using new MaterialCause(material, block)
* Same as using new Cause<BlockMaterial>(material, block)
*
* @param block location of the event
* @return cause
Expand All @@ -422,6 +420,11 @@ public Cause<BlockMaterial> toCause(Block block) {
return new MaterialCause<>(this, block);
}

/**
* Gets a {@link Set} of all {@link BlockComponent}s attached to this BlockMaterial.
*
* @return all attached Block Components
*/
public Set<Class<? extends BlockComponent>> getComponents() {
return components;
}
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/com/flowpowered/api/material/Material.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,16 @@ public final Material[] getSubMaterials() {
if (submaterialsDirty) {
int materialCount = 0;
Material[] sm = subMaterials.get();
for (int i = 0; i < sm.length; i++) {
if (sm[i] != null) {
for (Material aSm : sm) {
if (aSm != null) {
materialCount++;
}
}
Material[] newSubmaterials = new Material[materialCount];
materialCount = 0;
for (int i = 0; i < sm.length; i++) {
if (sm[i] != null) {
newSubmaterials[materialCount++] = sm[i];
for (Material aSm : sm) {
if (aSm != null) {
newSubmaterials[materialCount++] = aSm;
}
}
this.submaterialsContiguous = newSubmaterials;
Expand Down Expand Up @@ -332,11 +332,7 @@ public boolean isMaterial(Material... materials) {

@Override
public boolean equals(Object other) {
if (other instanceof Material) {
return other == this;
} else {
return false;
}
return other instanceof Material && other == this;
}

@Override
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/com/flowpowered/api/material/MaterialRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import com.flowpowered.math.GenericMath;

/**
* Handles all registered materials on the server statically.
* Statically handles all server-side registered materials.
*/
public abstract class MaterialRegistry {
private final static ConcurrentHashMap<String, Material> nameLookup = new ConcurrentHashMap<>(1000);
Expand Down Expand Up @@ -73,11 +73,16 @@ public static void setupClient() {
if (setup) {
throw new IllegalStateException("Can not setup material registry twice!");
}
materialRegistry = SyncedStringMap.create(null, new MemoryStore<Integer>(), 1, Short.MAX_VALUE, Material.class.getName());
materialRegistry = SyncedStringMap.create(null, new MemoryStore<>(), 1, Short.MAX_VALUE, Material.class.getName());

setup = true;
}

/**
* Checks whether the MaterialRegistry has been set up. If this returns {@code true}, further attempts to set up the registry will result in {@link IllegalStateException}.
*
* @return whether the MaterialRegistry has been set up
*/
public static boolean isSetup() {
return setup;
}
Expand Down Expand Up @@ -172,7 +177,7 @@ public static Material get(BlockFullState state) {
/**
* Gets the material for the given packed full state
*
* @param state the full state of the block
* @param packedState the packed state of the block
* @return Material of the id
*/
public static BlockMaterial get(int packedState) {
Expand All @@ -195,9 +200,9 @@ public static BlockMaterial get(int packedState) {
public static Material[] values() {
//TODO: This is wrong, need to count # of registered materials
HashSet<Material> set = new HashSet<>(1000);
for (int i = 0; i < materialLookup.length; i++) {
if (materialLookup[i].get() != null) {
set.add(materialLookup[i].get()[0]);
for (AtomicReference<Material[]> aMaterialLookup : materialLookup) {
if (aMaterialLookup.get() != null) {
set.add(aMaterialLookup.get()[0]);
}
}
return set.toArray(new Material[0]);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/flowpowered/api/material/Placeable.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
public interface Placeable {
/**
* Called when this block is about to be placed (before {@link #onPlacement(Block, short, BlockFace, boolean)}), checking if placement is allowed or not.
* Called when this block is about to be placed (before {@link #onPlacement(Block, short, BlockFace, Vector3f, boolean, Cause)}), checking if placement is allowed or not.
*
* @param block to place
* @param data block data to use during placement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import gnu.trove.map.hash.TIntObjectHashMap;

import com.flowpowered.math.imaginary.Quaternionf;
import com.flowpowered.math.vector.Vector3f;
import com.flowpowered.math.vector.Vector3i;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public int indexOf(BlockFace face, int def) {
*
* @param from the BlockFace to count
* @return the face at the offset
* @see BlockFaces.previous(BlockFace from, int offset);
* @see BlockFaces#previous(BlockFace, int);
*/
public BlockFace previous(BlockFace from) {
return previous(from, 1);
Expand All @@ -273,7 +273,7 @@ public BlockFace previous(BlockFace from) {
* @param from the BlockFace to count
* @param offset index in this range
* @return the face at the offset
* @see BlockFaces.next(BlockFace from, int offset);
* @see BlockFaces#next(BlockFace from, int offset);
*/
public BlockFace previous(BlockFace from, int offset) {
return this.next(from, -offset);
Expand All @@ -284,7 +284,7 @@ public BlockFace previous(BlockFace from, int offset) {
*
* @param from the BlockFace to count
* @return the face at the offset
* @see BlockFaces.next(BlockFace from, int offset);
* @see BlockFaces#next(BlockFace, int);
*/
public BlockFace next(BlockFace from) {
return next(from, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package com.flowpowered.api.material.block;

import com.flowpowered.api.geo.World;
import com.flowpowered.api.geo.WorldManager;
import com.flowpowered.api.geo.cuboid.Block;
import com.flowpowered.api.geo.reference.WorldReference;
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/flowpowered/api/player/PlayerNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import com.flowpowered.api.Client;
import com.flowpowered.api.geo.LoadOption;
Expand All @@ -43,7 +44,7 @@
import com.flowpowered.networking.session.Session;

/**
* The networking behind {@link org.spout.api.entity.Player}s. This component holds the {@link Session} which is the connection the Player has to the server.
* The networking behind {@link Player}s. This component holds the {@link Session} which is the connection the Player has to the server.
* This class also syncs *loaded* chunks to the client. It makes no attempt to load or generate chunks.
*/
public class PlayerNetwork {
Expand Down Expand Up @@ -92,9 +93,8 @@ public void setRepositionManager(RepositionManager rm) {
}

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

public void removeChunks(Set<Chunk> toRemove) {
Expand All @@ -111,9 +111,7 @@ public void preSnapshotRun(Transform transform) {
}

// We want to free all chunks first
for (ChunkReference ref : chunkFreeQueue) {
freeChunk(ref);
}
chunkFreeQueue.forEach(this::freeChunk);
chunkFreeQueue.clear();

// We will sync old chunks, but not new ones
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public interface TaskManager {
* @param task the task to execute
* @param delay the delay, in ms, before the task starts
* @param period the repeat period, in ms, of the task, or <= 0 to indicate a single shot task
* @return
* @return the {@link Task} object for the newly scheduled task
*/
public Task runTaskTimerAsynchronously(Object owner, Runnable task, long delay, long period);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/flowpowered/api/util/SyncedMapEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.flowpowered.events.object.ObjectEvent;

/**
* Event called when modifications occur on a StringMap
* Event called when modifications occur on a SyncedStringMap
*/
public class SyncedMapEvent extends ObjectEvent<SyncedStringMap> {
public static enum Action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void handleUpdate(SyncedMapEvent message) {
@Override
public void clear() {
super.clear();
callEvent(new SyncedMapEvent(this, SyncedMapEvent.Action.SET, new ArrayList<Pair<Integer, String>>()));
callEvent(new SyncedMapEvent(this, SyncedMapEvent.Action.SET, new ArrayList<>()));
}

public int getId() {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/flowpowered/api/util/UnloadSavable.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package com.flowpowered.api.util;

/**
* Represents something which can be unloaded and saved within the engine.
*/
public interface UnloadSavable {
/**
* Performs the necessary tasks to unload this object.
Expand Down
Loading

0 comments on commit 7cfe9e8

Please sign in to comment.