Skip to content

Commit

Permalink
Fix checkstyle violations in core
Browse files Browse the repository at this point in the history
Fixes all error violations in the Core project and also refactors some
inner classes inside the debug.BenchmarkScreen to standalone classes.
  • Loading branch information
Tropid committed Nov 26, 2016
1 parent 12f7ec3 commit 10b7ec0
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 145 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.terasology.core.debug;

/**
* Describes an instance of a benchmark which can be run.
*
* <p>The reason for not simply using the {@link Runnable} interface is that we
* may need something like a {@code prepareStep()} or {@code cleanupStep()}
* method, which must not be measured, in the future.</p>
*/
abstract class AbstractBenchmarkInstance {

public abstract void runStep();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import org.terasology.context.Context;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.logic.players.LocalPlayer;
import org.terasology.math.ChunkMath;
import org.terasology.math.Region3i;
import org.terasology.math.geom.Vector3f;
Expand All @@ -29,25 +28,18 @@
import org.terasology.rendering.nui.widgets.UIButton;
import org.terasology.rendering.nui.widgets.UIDropdownScrollable;
import org.terasology.rendering.nui.widgets.UIText;
import org.terasology.world.WorldProvider;
import org.terasology.world.block.Block;
import org.terasology.world.block.BlockManager;
import org.terasology.world.chunks.ChunkConstants;

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

/**
* This screen can be shown with the showScreen command in order to measure the performance of the block placement.
*/
public class BenchmarkScreen extends BaseInteractionScreen {
private static final int BLOCKS_PER_CHUNK = ChunkConstants.CHUNK_SIZE.x * ChunkConstants.CHUNK_SIZE.y
* ChunkConstants.CHUNK_SIZE.z;
private static final int DEFAULT_ITERATION_COUNT = 200;
static final int DEFAULT_ITERATION_COUNT = 200;

private UIText textArea;
private UIButton closeButton;
Expand Down Expand Up @@ -106,125 +98,6 @@ public void set(Object value) {

}

/**
* Benchmark types the user can select and start.
*/
private enum BenchmarkType {
WORLD_PROVIDER_SET_BLOCK("WorldProvider.setBlock", DEFAULT_ITERATION_COUNT) {
@Override
public AbstractBenchmarkInstance createInstance(Context context) {
return new BlockPlacementBenchmark(context, false);
}

@Override
public String getDescription() {
return "Uses setBlock of WorldProvder to replace the chunk (" + BLOCKS_PER_CHUNK + " blocks) above" +
" the player one iteration with stone the other iteration with air";

}
},

WORLD_PROVIDER_SET_BLOCKs("WorldProvider.setBlocks", DEFAULT_ITERATION_COUNT) {
@Override
public AbstractBenchmarkInstance createInstance(Context context) {
return new BlockPlacementBenchmark(context, true);
}

@Override
public String getDescription() {
return "Uses setBlocks of WorldProvder to replace the chunk (" + BLOCKS_PER_CHUNK + " blocks) above" +
" the player one iteration with stone the other iteration with air";

}
};

private String title;
private int maxIterations;

private BenchmarkType(String title, int maxIterations) {
this.title = title;
this.maxIterations = maxIterations;
}

@Override
public String toString() {
return title;
}

/**
* @return a runnable that will be invoked each iteration, and duration of the invokation will be recorded.
*/
public abstract AbstractBenchmarkInstance createInstance(Context context);

/**
*
* @return a description of the benchmark type.
*/
public abstract String getDescription();

public String getTitle() {
return title;
}

public int getMaxIterations() {
return maxIterations;
}
}

/**
* Can banchmark either {@link WorldProvider#setBlock(Vector3i, Block)} or {@link WorldProvider#setBlocks(Map)}
* depending on a constructor argument.
*/
private static class BlockPlacementBenchmark extends AbstractBenchmarkInstance {
private final WorldProvider worldProvider;
private final Region3i region3i;
private final Block air;
private final Block stone;
private final boolean useSetBlocksInsteadOfSetBlock;
private Block blockToPlace;

public BlockPlacementBenchmark(Context context, boolean useSetBlocksInsteadOfSetBlock) {
this.worldProvider = context.get(org.terasology.world.WorldProvider.class);
LocalPlayer localPlayer = context.get(LocalPlayer.class);
this.region3i = getChunkRegionAbove(localPlayer.getPosition());
BlockManager blockManager = context.get(BlockManager.class);
this.stone = blockManager.getBlock("Core:Stone");
this.useSetBlocksInsteadOfSetBlock = useSetBlocksInsteadOfSetBlock;
this.air = blockManager.getBlock("engine:air");
blockToPlace = stone;
}

@Override
public void runStep() {
if (useSetBlocksInsteadOfSetBlock) {
Map<Vector3i,Block> blocksToPlace = new HashMap<>();
for (Vector3i v : region3i) {
blocksToPlace.put(v, blockToPlace);
}
worldProvider.setBlocks(blocksToPlace);
} else {
for (Vector3i v : region3i) {
worldProvider.setBlock(v, blockToPlace);
}
}
if (blockToPlace == stone) {
blockToPlace = air;
} else {
blockToPlace = stone;
}
}
}


/**
* The interface runnable does not get used to have the possiblity in future to introduce a prepareStep and
* cleanupStep method. Those methods could be used in future to do stuff that mustnot be measured.
*/
private static abstract class AbstractBenchmarkInstance {

public abstract void runStep();
}

private void onStartStopButton(UIWidget uiWidget) {
if (runningBenchmark == null) {
handleBenchmarkStart();
Expand All @@ -239,7 +112,7 @@ private void handleBenchmarkStart() {
sum = 0;
min = Double.MAX_VALUE;
max = Double.MIN_VALUE;
sortedDurations.clear();;
sortedDurations.clear();
updateStartStopButton();
}

Expand Down Expand Up @@ -269,7 +142,7 @@ private void onCloseButton(UIWidget uiWidget) {
getManager().popScreen();
}

private static Region3i getChunkRegionAbove(Vector3f location) {
static Region3i getChunkRegionAbove(Vector3f location) {
Vector3i charecterPos = new Vector3i(location);
Vector3i chunkAboveCharacter = ChunkMath.calcChunkPos(charecterPos);
chunkAboveCharacter.addY(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.terasology.core.debug;

import org.terasology.context.Context;
import org.terasology.world.chunks.ChunkConstants;

/**
* Benchmark types the user can select and start.
*/
enum BenchmarkType {
WORLD_PROVIDER_SET_BLOCK("WorldProvider.setBlock", BenchmarkScreen.DEFAULT_ITERATION_COUNT) {
@Override
public AbstractBenchmarkInstance createInstance(Context context) {
return new BlockPlacementBenchmark(context, false);
}

@Override
public String getDescription() {
return "Uses setBlock of WorldProvder to replace the chunk (" + BLOCKS_PER_CHUNK + " blocks) above" +
" the player one iteration with stone the other iteration with air";

}
},

WORLD_PROVIDER_SET_BLOCKs("WorldProvider.setBlocks", BenchmarkScreen.DEFAULT_ITERATION_COUNT) {
@Override
public AbstractBenchmarkInstance createInstance(Context context) {
return new BlockPlacementBenchmark(context, true);
}

@Override
public String getDescription() {
return "Uses setBlocks of WorldProvder to replace the chunk (" + BLOCKS_PER_CHUNK + " blocks) above" +
" the player one iteration with stone the other iteration with air";

}
};

private static final int BLOCKS_PER_CHUNK = ChunkConstants.CHUNK_SIZE.x * ChunkConstants.CHUNK_SIZE.y
* ChunkConstants.CHUNK_SIZE.z;

private String title;
private int maxIterations;

private BenchmarkType(String title, int maxIterations) {
this.title = title;
this.maxIterations = maxIterations;
}

@Override
public String toString() {
return title;
}

/**
* @return a runnable that will be invoked each iteration, and duration of the invokation will be recorded.
*/
public abstract AbstractBenchmarkInstance createInstance(Context context);

/**
*
* @return a description of the benchmark type.
*/
public abstract String getDescription();

public String getTitle() {
return title;
}

public int getMaxIterations() {
return maxIterations;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.terasology.core.debug;

import java.util.HashMap;
import java.util.Map;

import org.terasology.context.Context;
import org.terasology.logic.players.LocalPlayer;
import org.terasology.math.Region3i;
import org.terasology.math.geom.Vector3i;
import org.terasology.world.WorldProvider;
import org.terasology.world.block.Block;
import org.terasology.world.block.BlockManager;

/**
* Can benchmark either {@link WorldProvider#setBlock(Vector3i, Block)} or {@link WorldProvider#setBlocks(Map)}
* depending on a constructor argument.
*/
class BlockPlacementBenchmark extends AbstractBenchmarkInstance {
private final WorldProvider worldProvider;
private final Region3i region3i;
private final Block air;
private final Block stone;
private final boolean useSetBlocksInsteadOfSetBlock;
private Block blockToPlace;

public BlockPlacementBenchmark(Context context, boolean useSetBlocksInsteadOfSetBlock) {
this.worldProvider = context.get(org.terasology.world.WorldProvider.class);
LocalPlayer localPlayer = context.get(LocalPlayer.class);
this.region3i = BenchmarkScreen.getChunkRegionAbove(localPlayer.getPosition());
BlockManager blockManager = context.get(BlockManager.class);
this.stone = blockManager.getBlock("Core:Stone");
this.useSetBlocksInsteadOfSetBlock = useSetBlocksInsteadOfSetBlock;
this.air = blockManager.getBlock("engine:air");
blockToPlace = stone;
}

@Override
public void runStep() {
if (useSetBlocksInsteadOfSetBlock) {
Map<Vector3i,Block> blocksToPlace = new HashMap<>();
for (Vector3i v : region3i) {
blocksToPlace.put(v, blockToPlace);
}
worldProvider.setBlocks(blocksToPlace);
} else {
for (Vector3i v : region3i) {
worldProvider.setBlock(v, blockToPlace);
}
}
if (blockToPlace == stone) {
blockToPlace = air;
} else {
blockToPlace = stone;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ public void onDropItemRequest(DropItemRequest event, EntityRef character) {

pickupItem.send(new DropItemEvent(event.getNewPosition()));

if (pickupItem.hasComponent(PickupComponent.class))
{
if (pickupItem.hasComponent(PickupComponent.class)) {
PickupComponent pickupComponent = pickupItem.getComponent(PickupComponent.class);
pickupComponent.timeDropped = time.getGameTimeInMs();
pickupItem.saveComponent(pickupComponent);
Expand Down Expand Up @@ -187,8 +186,7 @@ public void onDropItemRequest(DropItemButton event, EntityRef entity) {

Vector3f maxAllowedDistanceInDirection = direction.mul(1.5f);
HitResult hitResult = physics.rayTrace(position,direction,1.5f, StandardCollisionGroup.CHARACTER, StandardCollisionGroup.WORLD);
if (hitResult.isHit())
{
if (hitResult.isHit()) {
Vector3f possibleNewPosition = hitResult.getHitPoint();
maxAllowedDistanceInDirection = possibleNewPosition.sub(position);
}
Expand Down Expand Up @@ -240,8 +238,7 @@ public void onGiveItemToEntity(GiveItemEvent event, EntityRef entity) {
}

@ReceiveEvent(netFilter = RegisterMode.CLIENT)
public void onPlayerDeath(DeathEvent event, EntityRef entity)
{
public void onPlayerDeath(DeathEvent event, EntityRef entity) {
resetDropMark();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public DropItemRequest(EntityRef usedItem, EntityRef inventoryEntity, Vector3f i
this.count = count;
}

public DropItemRequest(EntityRef usedItem, EntityRef inventoryEntity, Vector3f impulse, Vector3f newPosition)
{
public DropItemRequest(EntityRef usedItem, EntityRef inventoryEntity, Vector3f impulse, Vector3f newPosition) {
this(usedItem, inventoryEntity, impulse, newPosition, 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.addInteractionRegion(interactionListener);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ public boolean onMouseClick(NUIMouseClickEvent event) {
int stackSize = InventoryUtils.getStackCount(getTargetItem());
if (stackSize > 0 && getTransferItem().getClass() != getTargetItem().getClass()) {
giveAmount((stackSize + 1) / 2);
}
else {
int transferStackSize = InventoryUtils.getStackCount(getTransferItem());
if (transferStackSize > 0) {
takeAmount(1);
}
} else {
int transferStackSize = InventoryUtils.getStackCount(getTransferItem());
if (transferStackSize > 0) {
takeAmount(1);
}
}
}
return true;
Expand Down

0 comments on commit 10b7ec0

Please sign in to comment.