Skip to content

Commit

Permalink
Use logger at more places.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Zangl committed Jan 12, 2016
1 parent e438d13 commit 4c3aa2f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
import java.util.LinkedList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

import net.famzangl.minecraft.minebot.ai.AIHelper;
import net.famzangl.minecraft.minebot.ai.command.AIChatController;
import net.famzangl.minecraft.minebot.ai.path.TaskReceiver;
Expand All @@ -41,6 +46,11 @@
*/
public abstract class TaskStrategy extends AIStrategy implements
TaskOperations, TaskReceiver {
private static final Marker MARKER_PREFACING = MarkerManager
.getMarker("preface");
private static final Marker MARKER_TASK = MarkerManager
.getMarker("task");
private static final Logger LOGGER = LogManager.getLogger(AIStrategy.class);
private static final int MAX_LOOKAHEAD = 9;
private static final int DESYNC_TIME = 5;
// Maximum distance for aiming at blocks to destroy.
Expand Down Expand Up @@ -72,8 +82,8 @@ public void addTask(AITask task) {

@Override
public void desync(TaskError error) {
System.out.println("Desync. This is an error. Did the server lag?");
System.out.println("Error: " + error);
LOGGER.error(MARKER_TASK, "Task sent desync. This is an error. Did the server lag?");
LOGGER.error(MARKER_TASK, "Error: " + error);
Thread.dumpStack();

if (!LESS_ERRORS || !lastErrors.contains(error)) {
Expand All @@ -91,7 +101,7 @@ public boolean faceAndDestroyForNextTask() {
boolean found = false;
for (int i = 1; i < MAX_LOOKAHEAD && i < tasks.size() && !found; i++) {
final AITask task = tasks.get(i);
// System.out.println("Prefetching with: " + task);
LOGGER.trace(MARKER_PREFACING, "Prefetching with: " + task);
if (tasks.get(i).getClass()
.isAnnotationPresent(SkipWhenSearchingPrefetch.class)) {
continue;
Expand All @@ -109,10 +119,8 @@ public boolean faceAndDestroyForNextTask() {
break;
}
}
// System.out.println("Prefacing: " + found + " for " +
// positions);
} else {
// System.out.println("Prefetching showstopper: " + task);
LOGGER.trace(MARKER_PREFACING, "Prefetching showstopper: " + task);
break;
}
}
Expand Down Expand Up @@ -140,7 +148,7 @@ protected TickResult onGameTick(AIHelper helper) {
t.onCanceled();
}
tasks.clear();
System.out.println("Waiting because of desync... " + desyncTimer);
LOGGER.debug(MARKER_TASK, "Waiting because of desync... " + desyncTimer);
desyncTimer--;
// pause for a tick, to reset all buttons, jump, ...
return TickResult.TICK_HANDLED;
Expand All @@ -150,7 +158,7 @@ protected TickResult onGameTick(AIHelper helper) {
activeTask = null;
searchAndPrintTasks(helper);
if (tasks.isEmpty()) {
System.out.println("No more tasks found.");
LOGGER.debug(MARKER_TASK, "No more tasks found.");
return TickResult.NO_MORE_WORK;
}
taskTimeout = 0;
Expand All @@ -163,19 +171,19 @@ protected TickResult onGameTick(AIHelper helper) {

final AITask task = tasks.peekFirst();
if (task.isFinished(helper)) {
System.out.println("Task done: " + task);
LOGGER.trace(MARKER_TASK,"Task done: " + task);
tasks.removeFirst();
System.out.println("Next will be: " + tasks.peekFirst());
LOGGER.debug(MARKER_TASK,"Next task will be: " + tasks.peekFirst());
taskTimeout = 0;
activeTask = null;
return TickResult.TICK_AGAIN;
} else {
int tickTimeout = task.getGameTickTimeout(helper);
if (taskTimeout > tickTimeout) {
LOGGER.error(MARKER_TASK, "Task timeout for: " + task);
desync(new StringTaskError(
"Task timed out. It should have been completed in "
+ (tickTimeout / 20f) + "s"));
System.out.println("Task was: " + task);
activeTask = null;
return TickResult.TICK_HANDLED;
} else {
Expand All @@ -192,7 +200,7 @@ protected TickResult onGameTick(AIHelper helper) {
private void searchAndPrintTasks(AIHelper helper) {
searchTasks(helper);
if (!tasks.isEmpty()) {
System.out.println("Found " + tasks.size() + " tasks, first task: "
LOGGER.trace(MARKER_TASK, "Found " + tasks.size() + " tasks, first task: "
+ tasks.peekFirst());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.util.Collections;
import java.util.List;

import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

import net.famzangl.minecraft.minebot.ai.AIHelper;
import net.famzangl.minecraft.minebot.ai.path.world.BlockSets;
import net.famzangl.minecraft.minebot.ai.path.world.WorldData;
Expand All @@ -43,6 +46,7 @@
*
*/
public class DestroyInRangeTask extends AITask implements CanPrefaceAndDestroy {
private static final Marker MARKER_DESTROY_IN_RANGE = MarkerManager.getMarker("destroy_in_range");
private class ClosestBlockFinder implements AreaVisitor {
BlockPos next = null;
double currentMin = Float.POSITIVE_INFINITY;
Expand All @@ -64,6 +68,7 @@ public void visit(WorldData world, int x, int y, int z) {

private class ApplyToDelta implements AreaVisitor {


public ApplyToDelta() {
}

Expand All @@ -72,8 +77,13 @@ public void visit(WorldData world, int x, int y, int z) {
if (isSafeToDestroy(world, x, y, z)) {
// FIXME: Use generics instead of cast.
((WorldWithDelta) world).setBlock(x, y, z, 0, 0);
y++;
while (isSafeFallingBlock(world, x, y, z)) {
((WorldWithDelta) world).setBlock(x, y, z, 0, 0);
y++;
}
} else {
System.out.println("No destruction for " + x + "," + y + ","
LOGGER.error(MARKER_DESTROY_IN_RANGE, "Cannot destroy for " + x + "," + y + ","
+ z + ", block is: " + world.getBlockId(x, y, z));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*******************************************************************************/
package net.famzangl.minecraft.minebot.ai.task;

import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

import net.famzangl.minecraft.minebot.ai.AIHelper;
import net.famzangl.minecraft.minebot.ai.net.NetworkHelper;
import net.minecraft.entity.projectile.EntityFishHook;
Expand All @@ -28,6 +31,7 @@
*
*/
public class DoFishTask extends AITask {
private static final Marker MARKER_FISH = MarkerManager.getMarker("fish");

private boolean revoked;
private int rightMotion = 2;
Expand Down Expand Up @@ -83,7 +87,7 @@ private boolean fishIsCapturedSP(AIHelper helper) {
return false;
}
if (fishEntity.motionY < -0.05) {
System.out.println(fishEntity.motionY + ", " + fishEntity.posY);
LOGGER.trace(MARKER_FISH, "Fish motion: " + fishEntity.motionY + ", " + fishEntity.posY);
rightMotion--;
} else {
rightMotion = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*******************************************************************************/
package net.famzangl.minecraft.minebot.ai.task.inventory;

import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

import net.famzangl.minecraft.minebot.ai.AIHelper;
import net.famzangl.minecraft.minebot.ai.ItemFilter;
import net.famzangl.minecraft.minebot.ai.task.AITask;
Expand All @@ -37,6 +40,7 @@
*/
@SkipWhenSearchingPrefetch
public class GetOnHotBarTask extends AITask {
private static final Marker MARKER_GET_ON_HOTBAR = MarkerManager.getMarker("get_on_hotbar");
private final ItemFilter itemFiler;
private boolean inventoryOpened;

Expand All @@ -63,7 +67,7 @@ public void runTick(AIHelper h, TaskOperations o) {
|| !itemFiler.matches(stack)) {
continue;
}
System.out.println("Swapping inventory slot " + i);
LOGGER.trace(MARKER_GET_ON_HOTBAR, "Swapping inventory slot " + i);
swap(h, screen, i);
h.getMinecraft().displayGuiScreen(null);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public void runTick(AIHelper h, TaskOperations o) {
h.faceAndDestroy(pos.add(0, 1, 0));
} else if (!BlockSets.AIR.isAt(world, pos)) {
if (!h.isStandingOn(pos.add(0, 1, 0))) {
System.out.println("Not standing on the right block.");
o.desync(new PositionTaskError(pos.add(0, 1, 0)));
}
if (hardBlocks.isAt(world, pos)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*******************************************************************************/
package net.famzangl.minecraft.minebot.ai.task.place;

import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

import net.famzangl.minecraft.minebot.ai.AIHelper;
import net.famzangl.minecraft.minebot.ai.ItemFilter;
import net.famzangl.minecraft.minebot.ai.path.world.BlockSets;
Expand All @@ -34,6 +37,7 @@
*
*/
public class JumpingPlaceAtHalfTask extends JumpingPlaceBlockAtFloorTask {
private static final Marker MARKER_JUMPING_PLACE_HALF = MarkerManager.getMarker("jumping_place_half");

public final static EnumFacing[] TRY_FOR_LOWER = new EnumFacing[] {
EnumFacing.DOWN, EnumFacing.EAST, EnumFacing.NORTH,
Expand Down Expand Up @@ -70,7 +74,7 @@ protected EnumFacing[] getBuildDirs() {
}

protected boolean faceSideBlock(AIHelper h, EnumFacing dir) {
System.out.println("Facing side " + dir);
LOGGER.trace(MARKER_JUMPING_PLACE_HALF, "Facing side " + dir);
BlockPos facingBlock = getPlaceAtPos().offset(dir);
if (BlockSets.AIR.isAt(h.getWorld(), facingBlock)) {
return false;
Expand Down

0 comments on commit 4c3aa2f

Please sign in to comment.