diff --git a/Minebot/build.gradle b/Minebot/build.gradle index c77f4802..d1731c4c 100644 --- a/Minebot/build.gradle +++ b/Minebot/build.gradle @@ -1,5 +1,5 @@ -version = "0.2.4" +version = "0.3.0" // group= "net.famzangl.minecraft.minebot" // http://maven.apache.org/guides/mini/guide-naming-conventions.html sourceSets.main{ java{ diff --git a/Minebot/resources/net/famzangl/minecraft/minebot/minebot.properties b/Minebot/resources/net/famzangl/minecraft/minebot/minebot.properties index 196aa8a0..d54f0b78 100644 --- a/Minebot/resources/net/famzangl/minecraft/minebot/minebot.properties +++ b/Minebot/resources/net/famzangl/minecraft/minebot/minebot.properties @@ -4,10 +4,24 @@ # Health levels at which the bot should do the action. 0..20 # 19 means that you got half a heart damage. -on_damage_logout_level = -1 +# Hint: Command may he a minebot command, this command then replaces the current command. +on_damage_logout_value = -1 + +on_damage_stop_value = -1 on_damage_command = /home -on_damage_command_level = -1 +on_damage_command_value = -1 + +# Same with distance an other player has to you. -1 disables. +on_player_comes_logout_value = -1 +on_player_comes_stop_value = -1 +on_player_comes_command = +on_player_comes_command_value = -1 +# Same with distance a creeper has to you. -1 disables. +on_creeper_comes_logout_value = -1 +on_creeper_comes_stop_value = -1 +on_creeper_comes_command = +on_creeper_comes_command_value = -1 # Distance bonus points for this ore mine_points_coal_ore = 1 @@ -34,7 +48,9 @@ mine_factor_emerald_ore = 12 # How much extra points two blocks above each other give us. # Extra points to add for double mining. mine_double_add = 2 -# How much random to use +# How much random to use 0 (disabled) .. 1 (a lot) +# This prevents two people who use the bot to do exactly the same. mine_randomness = 0.05 +# Blocks to use to build upwards. upwards_place_block = dirt,stone,cobblestone \ No newline at end of file diff --git a/Minebot/src/net/famzangl/minecraft/minebot/MinebotMod.java b/Minebot/src/net/famzangl/minecraft/minebot/MinebotMod.java index 3a889029..e341d8aa 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/MinebotMod.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/MinebotMod.java @@ -7,7 +7,7 @@ import cpw.mods.fml.common.Mod.Instance; import cpw.mods.fml.common.event.FMLInitializationEvent; -@Mod(modid = "minebot-mod", name = "Minebot", version = "0.2.4") +@Mod(modid = "minebot-mod", name = "Minebot", version = "0.3.0") public class MinebotMod { @Instance(value = "minebot-mod") public static MinebotMod instance; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/AIController.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/AIController.java index 31764531..3152ed67 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/AIController.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/AIController.java @@ -3,24 +3,16 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.Hashtable; -import java.util.LinkedList; -import java.util.List; import java.util.Map.Entry; import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.command.AIChatController; import net.famzangl.minecraft.minebot.ai.command.IAIControllable; -import net.famzangl.minecraft.minebot.ai.enchanting.EnchantStrategy; import net.famzangl.minecraft.minebot.ai.render.BuildMarkerRenderer; import net.famzangl.minecraft.minebot.ai.render.MarkingStrategy; import net.famzangl.minecraft.minebot.ai.render.PosMarkerRenderer; -import net.famzangl.minecraft.minebot.ai.strategy.LayRailStrategy; -import net.famzangl.minecraft.minebot.ai.strategy.LumberjackStrategy; -import net.famzangl.minecraft.minebot.ai.strategy.MineStrategy; -import net.famzangl.minecraft.minebot.ai.strategy.PlantStrategy; -import net.famzangl.minecraft.minebot.ai.task.AITask; -import net.famzangl.minecraft.minebot.ai.task.CanPrefaceAndDestroy; -import net.famzangl.minecraft.minebot.ai.task.SkipWhenSearchingPrefetch; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy.TickResult; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.ScaledResolution; import net.minecraft.client.settings.KeyBinding; @@ -53,9 +45,6 @@ * */ public class AIController extends AIHelper implements IAIControllable { - - private static final int TIMEOUT = 5 * 20; - private final static Hashtable uses = new Hashtable(); protected static final KeyBinding stop = new KeyBinding("Stop", @@ -63,36 +52,31 @@ public class AIController extends AIHelper implements IAIControllable { protected static final KeyBinding ungrab = new KeyBinding("Ungrab", Keyboard.getKeyIndex("U"), "Command Mod"); - private static final int MAX_LOOKAHEAD = 5; - static { - final KeyBinding mine = new KeyBinding("Farm ores", - Keyboard.getKeyIndex("K"), "Command Mod"); - final KeyBinding lumberjack = new KeyBinding("Farm wood", - Keyboard.getKeyIndex("J"), "Command Mod"); - final KeyBinding build_rail = new KeyBinding("Build Minecart tracks", - Keyboard.getKeyIndex("H"), "Command Mod"); - final KeyBinding mobfarm = new KeyBinding("Farm mobs", - Keyboard.getKeyIndex("M"), "Command Mod"); - final KeyBinding plant = new KeyBinding("Plant seeds", - Keyboard.getKeyIndex("P"), "Command Mod"); - uses.put(mine, new MineStrategy()); - uses.put(lumberjack, new LumberjackStrategy()); - uses.put(build_rail, new LayRailStrategy()); - uses.put(mobfarm, new EnchantStrategy()); - uses.put(plant, new PlantStrategy()); - ClientRegistry.registerKeyBinding(mine); - ClientRegistry.registerKeyBinding(lumberjack); - ClientRegistry.registerKeyBinding(build_rail); - ClientRegistry.registerKeyBinding(mobfarm); - ClientRegistry.registerKeyBinding(plant); + // final KeyBinding mine = new KeyBinding("Farm ores", + // Keyboard.getKeyIndex("K"), "Command Mod"); + // final KeyBinding lumberjack = new KeyBinding("Farm wood", + // Keyboard.getKeyIndex("J"), "Command Mod"); + // final KeyBinding build_rail = new KeyBinding("Build Minecart tracks", + // Keyboard.getKeyIndex("H"), "Command Mod"); + // final KeyBinding mobfarm = new KeyBinding("Farm mobs", + // Keyboard.getKeyIndex("M"), "Command Mod"); + // final KeyBinding plant = new KeyBinding("Plant seeds", + // Keyboard.getKeyIndex("P"), "Command Mod"); + // uses.put(mine, new MineStrategy()); + // uses.put(lumberjack, new LumberjackStrategy()); + // uses.put(build_rail, new LayRailStrategy()); + // uses.put(mobfarm, new EnchantStrategy()); + // uses.put(plant, new PlantStrategy()); + // ClientRegistry.registerKeyBinding(mine); + // ClientRegistry.registerKeyBinding(lumberjack); + // ClientRegistry.registerKeyBinding(build_rail); + // ClientRegistry.registerKeyBinding(mobfarm); + // ClientRegistry.registerKeyBinding(plant); ClientRegistry.registerKeyBinding(stop); ClientRegistry.registerKeyBinding(ungrab); } - private final LinkedList tasks = new LinkedList(); - private boolean desync; - private int timeout = 10 * 20; private boolean dead; private AIStrategy currentStrategy; @@ -107,29 +91,12 @@ public class AIController extends AIHelper implements IAIControllable { private boolean skipNextTick; - private boolean inUngrabMode; - private MouseHelper oldMouseHelper; private BuildMarkerRenderer buildMarkerRenderer; public AIController() { - new AIChatController(this); - } - - @Override - public void addTask(AITask task) { - if (task == null) { - throw new NullPointerException(); - } - tasks.add(task); - } - - @Override - public void desync() { - System.out.println("Desync. This is an error. Did the server lag?"); - Thread.dumpStack(); - desync = true; + AIChatController.getRegistry().setControlled(this); } /** @@ -156,67 +123,21 @@ public void onPlayerTick(ClientTickEvent evt) { AIStrategy newStrat; if (dead || stop.isPressed() || stop.getIsKeyPressed()) { + deactivateCurrentStrategy(); dead = false; - currentStrategy = null; - tasks.clear(); - System.out.println("New Strategy: None"); - resetTimeout(); } else if ((newStrat = findNewStrategy()) != null) { - tasks.clear(); + deactivateCurrentStrategy(); currentStrategy = newStrat; - System.out.println("New Strategy: " + newStrat); - resetTimeout(); - } else if (desync) { - tasks.clear(); + currentStrategy.setActive(true, this); } - desync = false; if (currentStrategy != null) { synchronized (strategyDescrMutex) { strategyDescr = currentStrategy.getDescription(); } - final AITask overrideTask = currentStrategy.getOverrideTask(this); - if (overrideTask != null) { - tasks.clear(); - tasks.push(overrideTask); - } else if (tasks.isEmpty()) { - currentStrategy.searchTasks(this); - resetTimeout(); - System.out.println("Found new task: " + tasks.peekFirst()); - } - - if (tasks.isEmpty()) { + final TickResult result = currentStrategy.gameTick(this); + if (result == TickResult.NO_MORE_WORK) { dead = true; - } else { - AITask task = tasks.get(0); - while (task.isFinished(this)) { - tasks.remove(0); - resetTimeout(); - System.out.println("Next task: " + tasks.peekFirst()); - if (tasks.peekFirst() != null) { - timeout = tasks.peekFirst().getGameTickTimeout(); - task = tasks.peekFirst(); - } else { - task = null; - break; - } - } - if (task == null) { - // pass - } else if (timeout <= 0) { - desync = true; - } else { - timeout--; - try { - task.runTick(this); - } catch (final Throwable t) { - t.printStackTrace(); - AIChatController - .addChatLine("Unexpected Error (" - + t.getMessage() - + "). Please report (and send the output on the console)!"); - } - } } } else { synchronized (strategyDescrMutex) { @@ -225,35 +146,11 @@ public void onPlayerTick(ClientTickEvent evt) { } } - @Override - public boolean faceAndDestroyForNextTask() { - boolean found = false; - for (int i = 1; i < MAX_LOOKAHEAD && i < tasks.size() && !found; i++) { - AITask task = tasks.get(i); - System.out.println("Prefetching with: " + task); - if (tasks.get(i).getClass() - .isAnnotationPresent(SkipWhenSearchingPrefetch.class)) { - continue; - } else if (task instanceof CanPrefaceAndDestroy) { - CanPrefaceAndDestroy dTask = (CanPrefaceAndDestroy) task; - List positions = dTask.getPredestroyPositions(this); - for (Pos pos : positions) { - if (!isAirBlock(pos.x, pos.y, pos.z)) { - faceAndDestroy(pos.x, pos.y, pos.z); - found = true; - break; - } - } - System.out.println("Prefacing: " + found + " for " + positions); - } else { - System.out.println("Prefetching showstopper: " + task); - break; - } - } - if (!found) { - System.out.println("Could not prefetch anything. " + tasks.size()); + private void deactivateCurrentStrategy() { + if (currentStrategy != null) { + currentStrategy.setActive(false, this); } - return found; + currentStrategy = null; } @SubscribeEvent @@ -275,8 +172,9 @@ public void drawHUD(RenderGameOverlayEvent.Post event) { try { // Dynamic 1.7.2 / 1.7.10 fix. ScaledResolution res; - Constructor method = ScaledResolution.class.getConstructors()[0]; - Object arg1 = method.getParameterTypes()[0] == Minecraft.class ? getMinecraft() + final Constructor method = ScaledResolution.class + .getConstructors()[0]; + final Object arg1 = method.getParameterTypes()[0] == Minecraft.class ? getMinecraft() : getMinecraft().gameSettings; res = (ScaledResolution) method.newInstance(arg1, getMinecraft().displayWidth, getMinecraft().displayHeight); @@ -290,11 +188,11 @@ public void drawHUD(RenderGameOverlayEvent.Post event) { res.getScaledWidth() - getMinecraft().fontRenderer.getStringWidth(str) - 10, 10, 16777215); - } catch (InstantiationException e) { + } catch (final InstantiationException e) { e.printStackTrace(); - } catch (IllegalAccessException e) { + } catch (final IllegalAccessException e) { e.printStackTrace(); - } catch (InvocationTargetException e) { + } catch (final InvocationTargetException e) { e.printStackTrace(); } } @@ -387,10 +285,6 @@ private AIStrategy findNewStrategy() { return null; } - private void resetTimeout() { - timeout = TIMEOUT; - } - @Override public AIHelper getAiHelper() { return this; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/AIHelper.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/AIHelper.java index 02af4b95..5d41b381 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/AIHelper.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/AIHelper.java @@ -5,7 +5,6 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.command.AIChatController; -import net.famzangl.minecraft.minebot.ai.task.AITask; import net.famzangl.minecraft.minebot.ai.task.BlockSide; import net.famzangl.minecraft.minebot.build.BuildManager; import net.minecraft.block.Block; @@ -130,20 +129,6 @@ public Block getBlock(Pos pos) { return getBlock(pos.x, pos.y, pos.z); } - /** - * Adds a task that should be executed. - * - * @param task - * The new task - */ - public abstract void addTask(AITask task); - - /** - * This should be called whenever the current task could not achieve it's - * goal. All following tasks are unscheduled. - */ - public abstract void desync(); - /** * Gets the pos1 marker. * @@ -257,7 +242,7 @@ public boolean selectCurrentItem(ItemFilter f) { * @return the position or null if it was not found. */ public Pos findBlock(Block blockType) { - Pos current = getPlayerPosition(); + final Pos current = getPlayerPosition(); Pos pos = null; for (int x = current.x - 2; x <= current.x + 2; x++) { for (int z = current.z - 2; z <= current.z + 2; z++) { @@ -388,7 +373,8 @@ public boolean isStandingOn(int x, int y, int z) { */ public Pos getPlayerPosition() { final int x = (int) Math.floor(getMinecraft().thePlayer.posX); - final int y = (int) Math.floor(getMinecraft().thePlayer.boundingBox.minY + 0.05); + final int y = (int) Math + .floor(getMinecraft().thePlayer.boundingBox.minY + 0.05); final int z = (int) Math.floor(getMinecraft().thePlayer.posZ); return new Pos(x, y, z); } @@ -795,13 +781,13 @@ protected void resetAllInputs() { } protected boolean userTookOver() { - MovementInput mi = resetMovementInput == null ? mc.thePlayer.movementInput + final MovementInput mi = resetMovementInput == null ? mc.thePlayer.movementInput : resetMovementInput; - KeyBinding attack = resetAttackKey == null ? mc.gameSettings.keyBindAttack + final KeyBinding attack = resetAttackKey == null ? mc.gameSettings.keyBindAttack : resetAttackKey; - KeyBinding use = resetUseItemKey == null ? mc.gameSettings.keyBindUseItem + final KeyBinding use = resetUseItemKey == null ? mc.gameSettings.keyBindUseItem : resetUseItemKey; - KeyBinding sneak = resetSneakKey == null ? mc.gameSettings.keyBindSneak + final KeyBinding sneak = resetSneakKey == null ? mc.gameSettings.keyBindSneak : resetSneakKey; return mi.moveForward != 0 || mi.moveStrafe != 0 || mi.jump @@ -1007,12 +993,12 @@ public boolean walkTowards(double x, double z, boolean jump, boolean face) { if (distTo < 4 * WALK_PER_STEP) { speed = Math.max(distTo / WALK_PER_STEP / 4, 0.1); } - double yaw = mc.thePlayer.rotationYaw / 180 * Math.PI; - double lookX = -Math.sin(yaw); - double lookZ = Math.cos(yaw); - double dlength = Math.sqrt(dx * dx + dz * dz); - double same = (lookX * dx + lookZ * dz) / dlength; - double strafe = (lookZ * dx - lookX * dz) / dlength; + final double yaw = mc.thePlayer.rotationYaw / 180 * Math.PI; + final double lookX = -Math.sin(yaw); + final double lookZ = Math.cos(yaw); + final double dlength = Math.sqrt(dx * dx + dz * dz); + final double same = (lookX * dx + lookZ * dz) / dlength; + final double strafe = (lookZ * dx - lookX * dz) / dlength; System.out.println("look: " + lookX + "," + lookZ + "; d = " + dx + "," + dz + "; walk: " + same + "," + strafe); final MovementInput movement = new MovementInput(); @@ -1119,22 +1105,15 @@ public static ForgeDirection getDirectionFor(Pos pos) { } public int getLightAt(Pos pos) { - Chunk chunk = mc.theWorld.getChunkFromChunkCoords(pos.x >> 4, pos.z >> 4); - ExtendedBlockStorage storage = chunk.getBlockStorageArray()[pos.y >> 4]; - if (storage == null) { - return 0; - } else { - return storage.getExtBlocklightValue(pos.x & 15, pos.y & 15, pos.z & 15); - } + final Chunk chunk = mc.theWorld.getChunkFromChunkCoords(pos.x >> 4, + pos.z >> 4); + final ExtendedBlockStorage storage = chunk.getBlockStorageArray()[pos.y >> 4]; + if (storage == null) { + return 0; + } else { + return storage.getExtBlocklightValue(pos.x & 15, pos.y & 15, + pos.z & 15); + } } - /** - * This can be called by the current task to do a look ahead and already let - * the next task to it's face and destroy. Use with care. - * - * @return - */ - public boolean faceAndDestroyForNextTask() { - return false; - } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/AIStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/AIStrategy.java deleted file mode 100644 index 0cd0e795..00000000 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/AIStrategy.java +++ /dev/null @@ -1,42 +0,0 @@ -package net.famzangl.minecraft.minebot.ai; - -import net.famzangl.minecraft.minebot.ai.task.AITask; - -/** - * An {@link AIStrategy} tells the bot what to do next. It should recover from - * any state that might happen while the bot executes. - * - * @author michael - * - */ -public interface AIStrategy { - - /** - * Searches for tasks to do. Always called in a game tick. The tasks should - * be added with {@link AIHelper#addTask(AITask)}. - *

- * You cannot make any assumptions on the state of the world. Tasks may even - * have been interrupted in between. You can be sure that there are no tasks - * currently added when this method is called. - * - * @param helper - * The helper that can be used. - */ - void searchTasks(AIHelper helper); - - /** - * - * @return A String to display in the top right hand corner of the screen. - */ - String getDescription(); - - /** - * get an emergency override task. - * - * @param aiController - * @return A not-null-Value to cancel all pending tasks and - * execute a new one instead. - */ - AITask getOverrideTask(AIHelper helper); - -} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/AIStrategyFactory.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/AIStrategyFactory.java index 280c97fa..0b5d8a08 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/AIStrategyFactory.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/AIStrategyFactory.java @@ -1,5 +1,7 @@ package net.famzangl.minecraft.minebot.ai; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; + public interface AIStrategyFactory { public AIStrategy produceStrategy(AIHelper helper); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/BlockItemFilter.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/BlockItemFilter.java index 5b667823..93c97554 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/BlockItemFilter.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/BlockItemFilter.java @@ -40,21 +40,25 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (obj == null) + } + if (obj == null) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; - BlockItemFilter other = (BlockItemFilter) obj; - if (!Arrays.equals(matched, other.matched)) + } + final BlockItemFilter other = (BlockItemFilter) obj; + if (!Arrays.equals(matched, other.matched)) { return false; + } return true; } public String getDescriptiveString() { - StringBuilder str = new StringBuilder(); - for (Block m : matched) { + final StringBuilder str = new StringBuilder(); + for (final Block m : matched) { if (str.length() > 0) { str.append(", "); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/ColoredBlockItemFilter.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/ColoredBlockItemFilter.java index d7b103b3..3a47fa18 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/ColoredBlockItemFilter.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/ColoredBlockItemFilter.java @@ -73,18 +73,22 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (!super.equals(obj)) + } + if (!super.equals(obj)) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; - ColoredBlockItemFilter other = (ColoredBlockItemFilter) obj; - if (colorMeta != other.colorMeta) + } + final ColoredBlockItemFilter other = (ColoredBlockItemFilter) obj; + if (colorMeta != other.colorMeta) { return false; + } return true; } - + @Override public String getDescriptiveString() { return COLORS[colorMeta] + " " + super.getDescriptiveString(); diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/MarkerRenderer.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/MarkerRenderer.java deleted file mode 100644 index 1e4cc0e9..00000000 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/MarkerRenderer.java +++ /dev/null @@ -1,92 +0,0 @@ -package net.famzangl.minecraft.minebot.ai; - -import net.famzangl.minecraft.minebot.Pos; -import net.minecraft.client.renderer.OpenGlHelper; -import net.minecraft.client.renderer.Tessellator; - -import org.lwjgl.opengl.GL11; - -/** - * Renders the marker boxes for indicating pos1/pos2 - * - * @author michael - * - */ -public class MarkerRenderer { - - private static final double MAX = 1.05; - private static final double MIN = -0.05; - - public void render(double x, double y, double z, Pos... markerPos) { - - GL11.glBlendFunc(GL11.GL_DST_COLOR, GL11.GL_SRC_COLOR); - GL11.glDisable(GL11.GL_TEXTURE_2D); - - OpenGlHelper.glBlendFunc(774, 768, 1, 0); - GL11.glColor4f(1.0F, 1.0F, 1.0F, 0.5F); - GL11.glPushMatrix(); - GL11.glPolygonOffset(-3.0F, -3.0F); - GL11.glEnable(GL11.GL_POLYGON_OFFSET_FILL); - GL11.glEnable(GL11.GL_ALPHA_TEST); - final boolean hadBlend = GL11.glIsEnabled(GL11.GL_BLEND); - GL11.glEnable(GL11.GL_BLEND); - final Tessellator tessellator = Tessellator.instance; - tessellator.startDrawingQuads(); - tessellator.setTranslation(-x, -y, -z); - - float redPart = 1.0f; - for (final Pos m : markerPos) { - tessellator.setColorRGBA_F(redPart, 0, 0, 0.5f); - if (m != null) { - renderMarker(m); - } - redPart *= 0.6; - } - - tessellator.draw(); - tessellator.setTranslation(0.0D, 0.0D, 0.0D); - GL11.glDisable(GL11.GL_ALPHA_TEST); - GL11.glPolygonOffset(0.0F, 0.0F); - GL11.glDisable(GL11.GL_POLYGON_OFFSET_FILL); - GL11.glEnable(GL11.GL_ALPHA_TEST); - if (!hadBlend) { - GL11.glDisable(GL11.GL_BLEND); - } - GL11.glEnable(GL11.GL_TEXTURE_2D); - GL11.glDepthMask(true); - GL11.glPopMatrix(); - } - - private void renderMarker(Pos m) { - final Tessellator tessellator = Tessellator.instance; - tessellator.addVertex(m.x + MIN, m.y + MAX, m.z + MIN); - tessellator.addVertex(m.x + MIN, m.y + MAX, m.z + MAX); - tessellator.addVertex(m.x + MAX, m.y + MAX, m.z + MAX); - tessellator.addVertex(m.x + MAX, m.y + MAX, m.z + MIN); - - tessellator.addVertex(m.x + MIN, m.y + MIN, m.z + MIN); - tessellator.addVertex(m.x + MIN, m.y + MIN, m.z + MAX); - tessellator.addVertex(m.x + MIN, m.y + MAX, m.z + MAX); - tessellator.addVertex(m.x + MIN, m.y + MAX, m.z + MIN); - - tessellator.addVertex(m.x + MAX, m.y + MAX, m.z + MIN); - tessellator.addVertex(m.x + MAX, m.y + MAX, m.z + MAX); - tessellator.addVertex(m.x + MAX, m.y + MIN, m.z + MAX); - tessellator.addVertex(m.x + MAX, m.y + MIN, m.z + MIN); - - tessellator.addVertex(m.x + MIN, m.y + MIN, m.z + MIN); - tessellator.addVertex(m.x + MIN, m.y + MAX, m.z + MIN); - tessellator.addVertex(m.x + MAX, m.y + MAX, m.z + MIN); - tessellator.addVertex(m.x + MAX, m.y + MIN, m.z + MIN); - - tessellator.addVertex(m.x + MIN, m.y + MAX, m.z + MAX); - tessellator.addVertex(m.x + MIN, m.y + MIN, m.z + MAX); - tessellator.addVertex(m.x + MAX, m.y + MIN, m.z + MAX); - tessellator.addVertex(m.x + MAX, m.y + MAX, m.z + MAX); - - tessellator.addVertex(m.x + MIN, m.y + MIN, m.z + MAX); - tessellator.addVertex(m.x + MIN, m.y + MIN, m.z + MIN); - tessellator.addVertex(m.x + MAX, m.y + MIN, m.z + MIN); - tessellator.addVertex(m.x + MAX, m.y + MIN, m.z + MAX); - } -} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/PathFinderField.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/PathFinderField.java index 5f8a9e96..12acf486 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/PathFinderField.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/PathFinderField.java @@ -156,12 +156,7 @@ public int compareTo(Dest o) { } - public final boolean searchSomethingAround(Pos playerPosition) { - return searchSomethingAround(playerPosition.x, playerPosition.y, - playerPosition.z); - } - - public boolean searchSomethingAround(int cx, int cy, int cz) { + protected boolean searchSomethingAround(int cx, int cy, int cz) { if (data.offsetX != cx - SIZE_X_Z / 2 || data.offsetY != cy - Y_LEVEL / 2 || data.offsetZ != cz - SIZE_X_Z / 2) { @@ -176,7 +171,7 @@ public boolean searchSomethingAround(int cx, int cy, int cz) { pq.clear(); final int start = getIndexForBlock(cx, cy, cz); pq.add(start); - float startRating = rateDestination(start); + final float startRating = rateDestination(start); setDistance(start, 1); if (startRating >= 0) { currentDest = new Dest(start, startRating); diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/animals/ThrowFishingRodTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/animals/ThrowFishingRodTask.java index 098bf2ac..c4057208 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/animals/ThrowFishingRodTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/animals/ThrowFishingRodTask.java @@ -2,6 +2,7 @@ import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.ItemFilter; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; import net.minecraft.item.ItemFishingRod; import net.minecraft.item.ItemStack; @@ -22,7 +23,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { if (h.selectCurrentItem(new FishingRodFilter())) { h.overrideUseItem(); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/AIChatController.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/AIChatController.java index d1d746be..3dd1f686 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/AIChatController.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/AIChatController.java @@ -9,6 +9,7 @@ import net.famzangl.minecraft.minebot.ai.commands.CommandHelp; import net.famzangl.minecraft.minebot.ai.commands.CommandKill; import net.famzangl.minecraft.minebot.ai.commands.CommandMine; +import net.famzangl.minecraft.minebot.ai.commands.CommandPause; import net.famzangl.minecraft.minebot.ai.commands.CommandPlant; import net.famzangl.minecraft.minebot.ai.commands.CommandRun; import net.famzangl.minecraft.minebot.ai.commands.CommandShear; @@ -29,6 +30,7 @@ import net.famzangl.minecraft.minebot.build.commands.CommandStepNext; import net.famzangl.minecraft.minebot.build.commands.CommandStepPlace; import net.famzangl.minecraft.minebot.build.commands.CommandStepWalk; +import net.minecraft.client.Minecraft; import net.minecraft.util.ChatComponentText; import com.google.common.base.Function; @@ -40,10 +42,6 @@ * */ public class AIChatController { - public static AIChatController runningInstance = null; - - private final IAIControllable controlled; - private static final CommandRegistry registry = new CommandRegistry(); private static final int PER_PAGE = 8; @@ -60,6 +58,7 @@ public class AIChatController { registerCommand(CommandTint.class); registerCommand(CommandBuildWay.class); registerCommand(CommandShear.class); + registerCommand(CommandPause.class); registerCommand(CommandKill.class); registerCommand(CommandFish.class); @@ -80,10 +79,7 @@ public class AIChatController { registerCommand(CommandCount.class); } - public AIChatController(IAIControllable c) { - controlled = c; - this.registry.setControlled(c); - runningInstance = this; + private AIChatController() { } private static void registerCommand(Class commandClass) { @@ -91,17 +87,15 @@ private static void registerCommand(Class commandClass) { } public static void addChatLine(String message) { - if (runningInstance != null) { - runningInstance.addToChat("[Minebot] " + message); - } + addToChat("[Minebot] " + message); } public static CommandRegistry getRegistry() { return registry; } - private void addToChat(String string) { - controlled.getMinecraft().thePlayer + private static void addToChat(String string) { + Minecraft.getMinecraft().thePlayer .addChatMessage(new ChatComponentText(string)); } @@ -111,8 +105,8 @@ public static void addToChatPaged(String title, int page, List data, + (int) Math.ceil((float) data.size() / PER_PAGE)); for (int i = Math.max(0, page - 1) * PER_PAGE; i < Math.min(page * PER_PAGE, data.size()); i++) { - String line = convert.apply(data.get(i)); - AIChatController.addChatLine(line); + final String line = convert.apply(data.get(i)); + addChatLine(line); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/AIHelperBuilder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/AIHelperBuilder.java index e283df32..b0cc77c6 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/AIHelperBuilder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/AIHelperBuilder.java @@ -14,7 +14,7 @@ public AIHelperBuilder(AICommandParameter annot) { public void addArguments(ArrayList list) { // ignored. } - + @Override public Object getParameter(AIHelper helper, String[] arguments) { return helper; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/ArgumentDefinition.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/ArgumentDefinition.java index d0b055de..638a9f05 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/ArgumentDefinition.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/ArgumentDefinition.java @@ -20,8 +20,7 @@ public ArgumentDefinition(String descriptionType) { this(descriptionType, ""); } - public ArgumentDefinition(String descriptionType, - String descriptionInfo) { + public ArgumentDefinition(String descriptionType, String descriptionInfo) { super(); this.descriptionType = descriptionType; this.descriptionInfo = descriptionInfo; @@ -43,7 +42,7 @@ public boolean couldEvaluateAgainst(String string) { } public final String getDescriptionString() { - String info = getDescriptionInfo(); + final String info = getDescriptionInfo(); return getDescriptionType() + (info == null || info.isEmpty() ? "" : ": " + info); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/BlockNameBuilder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/BlockNameBuilder.java index 237ed516..093fd1ea 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/BlockNameBuilder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/BlockNameBuilder.java @@ -9,7 +9,8 @@ public class BlockNameBuilder extends ParameterBuilder { - private final static class BlockArgumentDefinition extends ArgumentDefinition { + private final static class BlockArgumentDefinition extends + ArgumentDefinition { public BlockArgumentDefinition(String description) { super("Block", description); } @@ -24,19 +25,20 @@ public void getTabCompleteOptions(String currentStart, Collection addTo) { super.getTabCompleteOptions(currentStart, addTo); @SuppressWarnings("unchecked") - Set keys = Block.blockRegistry.getKeys(); - for (String k : keys) { + final Set keys = Block.blockRegistry.getKeys(); + for (final String k : keys) { if (k.startsWith(MINECRAFT_PREFIX)) { - String subKey = k.substring(MINECRAFT_PREFIX.length()); + final String subKey = k + .substring(MINECRAFT_PREFIX.length()); addKey(currentStart, addTo, subKey); } else { addKey(currentStart, addTo, k); - } + } } } - private void addKey(String currentStart, - Collection addTo, String subKey) { + private void addKey(String currentStart, Collection addTo, + String subKey) { if (subKey.startsWith(currentStart)) { addTo.add(subKey); } @@ -56,9 +58,10 @@ public void addArguments(ArrayList list) { @Override public Object getParameter(AIHelper helper, String[] arguments) { - Object block = Block.blockRegistry.getObject(arguments[0]); + final Object block = Block.blockRegistry.getObject(arguments[0]); if (block == null) { - throw new CommandEvaluationException("Block " + arguments[0] + " is unknown"); + throw new CommandEvaluationException("Block " + arguments[0] + + " is unknown"); } return block; } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/ColorNameBuilder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/ColorNameBuilder.java index 6e58b039..b02fed69 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/ColorNameBuilder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/ColorNameBuilder.java @@ -16,7 +16,7 @@ public ColorArgumentDefinition(String description) { @Override public boolean couldEvaluateAgainst(String string) { - for (String color : ColoredBlockItemFilter.COLORS) { + for (final String color : ColoredBlockItemFilter.COLORS) { if (color.equalsIgnoreCase(string)) { return true; } @@ -27,7 +27,7 @@ public boolean couldEvaluateAgainst(String string) { @Override public void getTabCompleteOptions(String currentStart, Collection addTo) { - for (String color : ColoredBlockItemFilter.COLORS) { + for (final String color : ColoredBlockItemFilter.COLORS) { if (color.toLowerCase().startsWith(currentStart.toLowerCase())) { addTo.add(color); } @@ -46,9 +46,9 @@ public void addArguments(ArrayList list) { @Override public Object getParameter(AIHelper helper, String[] arguments) { - String[] colors = ColoredBlockItemFilter.COLORS; + final String[] colors = ColoredBlockItemFilter.COLORS; for (int i = 0; i < colors.length; i++) { - String color = colors[i]; + final String color = colors[i]; if (color.equalsIgnoreCase(arguments[0])) { return i; } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/CommandDefinition.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/CommandDefinition.java index c097a86b..f1d45989 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/CommandDefinition.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/CommandDefinition.java @@ -7,7 +7,7 @@ import java.util.Arrays; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; /** * This is the basic definition of a command. @@ -32,7 +32,7 @@ public CommandDefinition(Method m) { if (types[parameter] == AIHelper.class) { b = new AIHelperBuilder(null); } else { - AICommandParameter annot = findParameterAnnotation(parameterAnnotations[parameter]); + final AICommandParameter annot = findParameterAnnotation(parameterAnnotations[parameter]); switch (annot.type()) { case BLOCK_NAME: b = new BlockNameBuilder(annot); @@ -71,7 +71,7 @@ public CommandDefinition(Method m) { } private AICommandParameter findParameterAnnotation(Annotation[] annotations) { - for (Annotation a : annotations) { + for (final Annotation a : annotations) { if (a instanceof AICommandParameter) { // TODO: Type check of parameter. return (AICommandParameter) a; @@ -107,26 +107,26 @@ public boolean couldEvaluateStartingWith(String[] arguments2) { } public AIStrategy evaluate(AIHelper helper, String[] args) { - Object[] params = new Object[builders.length]; + final Object[] params = new Object[builders.length]; for (int parameter = 0; parameter < builders.length; parameter++) { - String[] argsPart = Arrays.copyOfRange(args, + final String[] argsPart = Arrays.copyOfRange(args, parameterStarts[parameter], parameterStarts[parameter + 1]); params[parameter] = builders[parameter].getParameter(helper, argsPart); } try { - Object result = method.invoke(null, params); + final Object result = method.invoke(null, params); if (result instanceof AIStrategy) { return (AIStrategy) result; } else { return null; } - } catch (IllegalAccessException e) { + } catch (final IllegalAccessException e) { doThrow(e); - } catch (IllegalArgumentException e) { + } catch (final IllegalArgumentException e) { doThrow(e); - } catch (InvocationTargetException e) { - Throwable exception = e.getTargetException(); + } catch (final InvocationTargetException e) { + final Throwable exception = e.getTargetException(); doThrow(exception); } return null; @@ -134,7 +134,9 @@ public AIStrategy evaluate(AIHelper helper, String[] args) { private void doThrow(Throwable exception) { exception.printStackTrace(); - throw exception instanceof CommandEvaluationException ? (CommandEvaluationException) exception : new CommandEvaluationException("Unexpected error while evaluating.", exception); + throw exception instanceof CommandEvaluationException ? (CommandEvaluationException) exception + : new CommandEvaluationException( + "Unexpected error while evaluating.", exception); } public ArrayList getArguments() { diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/CommandNameBuilder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/CommandNameBuilder.java index c1df19c8..882643c5 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/CommandNameBuilder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/CommandNameBuilder.java @@ -15,9 +15,10 @@ public CommandArgumentDefinition(String description) { @Override public boolean couldEvaluateAgainst(String string) { - for (CommandDefinition command : AIChatController.getRegistry() - .getAllCommands()) { - ArrayList args = command.getArguments(); + for (final CommandDefinition command : AIChatController + .getRegistry().getAllCommands()) { + final ArrayList args = command + .getArguments(); if (args.get(0).couldEvaluateAgainst(string)) { return true; } @@ -28,9 +29,10 @@ public boolean couldEvaluateAgainst(String string) { @Override public void getTabCompleteOptions(String currentStart, Collection addTo) { - for (CommandDefinition command : AIChatController.getRegistry() - .getAllCommands()) { - ArrayList args = command.getArguments(); + for (final CommandDefinition command : AIChatController + .getRegistry().getAllCommands()) { + final ArrayList args = command + .getArguments(); args.get(0).getTabCompleteOptions(currentStart, addTo); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/CommandRegistry.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/CommandRegistry.java index 167d3514..0a147b9c 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/CommandRegistry.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/CommandRegistry.java @@ -10,7 +10,7 @@ import java.util.List; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.minecraft.command.CommandBase; import net.minecraft.command.ICommandSender; import net.minecraftforge.client.ClientCommandHandler; @@ -35,7 +35,7 @@ public void processCommand(ICommandSender sender, String[] args) { if (strategy != null) { controlled.requestUseStrategy(strategy); } - } catch (UnknownCommandException e) { + } catch (final UnknownCommandException e) { if (e.getEvaluateable().size() > 0) { AIChatController .addChatLine("ERROR: More than 1 command matches your command line."); @@ -44,10 +44,10 @@ public void processCommand(ICommandSender sender, String[] args) { AIChatController.addChatLine("ERROR: No command:" + combine(args) + "."); } - } catch (CommandEvaluationException e) { + } catch (final CommandEvaluationException e) { AIChatController.addChatLine("ERROR while evaluating: " + e.getMessage()); - } catch (Throwable e) { + } catch (final Throwable e) { e.printStackTrace(); AIChatController .addChatLine("ERROR: Could not evaluate. Please report."); @@ -55,8 +55,8 @@ public void processCommand(ICommandSender sender, String[] args) { } private String combine(String[] args) { - StringBuilder b = new StringBuilder(); - for (String a : args) { + final StringBuilder b = new StringBuilder(); + for (final String a : args) { b.append(" "); b.append(a); } @@ -94,7 +94,7 @@ public int getRequiredPermissionLevel() { public void register(Class commandClass) { checkCommandCLass(commandClass); - String name = commandClass.getAnnotation(AICommand.class).name(); + final String name = commandClass.getAnnotation(AICommand.class).name(); List list = commandTable.get(name); if (list == null) { list = new ArrayList(); @@ -118,9 +118,9 @@ private void checkCommandCLass(Class commandClass) { public AIStrategy evaluateCommand(AIHelper helper, String commandID, String[] arguments) throws UnknownCommandException { - List commands = getCommands(commandID); - ArrayList evaluateable = new ArrayList(); - for (CommandDefinition c : commands) { + final List commands = getCommands(commandID); + final ArrayList evaluateable = new ArrayList(); + for (final CommandDefinition c : commands) { if (c.couldEvaluateAgainst(arguments)) { evaluateable.add(c); } @@ -134,20 +134,21 @@ public AIStrategy evaluateCommand(AIHelper helper, String commandID, public List tabCompletion(AIHelper helper, String commandID, String[] currentArgs) { - List commands = getCommands(commandID); - HashSet suggestions = new HashSet(); - String[] fixedArgs = Arrays.copyOf(currentArgs, currentArgs.length - 1); - for (CommandDefinition c : commands) { - ArrayList args = c.getArguments(); + final List commands = getCommands(commandID); + final HashSet suggestions = new HashSet(); + final String[] fixedArgs = Arrays.copyOf(currentArgs, + currentArgs.length - 1); + for (final CommandDefinition c : commands) { + final ArrayList args = c.getArguments(); if (c.couldEvaluateStartingWith(fixedArgs) && args.size() > fixedArgs.length) { - ArgumentDefinition lastArg = c.getArguments().get( + final ArgumentDefinition lastArg = c.getArguments().get( currentArgs.length - 1); lastArg.getTabCompleteOptions( currentArgs[currentArgs.length - 1], suggestions); } } - ArrayList asList = new ArrayList(suggestions); + final ArrayList asList = new ArrayList(suggestions); Collections.sort(asList); return asList; } @@ -162,7 +163,7 @@ private List getCommands(String commandID) { private void getCommandsForClass(Class commandClass, List commands) { - for (Method m : commandClass.getMethods()) { + for (final Method m : commandClass.getMethods()) { if (Modifier.isStatic(m.getModifiers()) && m.isAnnotationPresent(AICommandInvocation.class)) { commands.add(getCommandForMethod(m)); @@ -175,13 +176,17 @@ private CommandDefinition getCommandForMethod(Method m) { } public List getAllCommands() { - ArrayList defs = new ArrayList(); - for (List list : commandTable.values()) { + final ArrayList defs = new ArrayList(); + for (final List list : commandTable.values()) { defs.addAll(list); } return defs; } + public IAIControllable getControlled() { + return controlled; + } + public void setControlled(IAIControllable controlled) { this.controlled = controlled; } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/EnumNameBuilder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/EnumNameBuilder.java index d1c0ea94..abf2a9d6 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/EnumNameBuilder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/EnumNameBuilder.java @@ -22,7 +22,7 @@ public EnumArgumentDefinition(String descriptionType, @Override public boolean couldEvaluateAgainst(String string) { - for (Enum type : options) { + for (final Enum type : options) { if (type.name().equalsIgnoreCase(string)) { return true; } @@ -33,7 +33,7 @@ public boolean couldEvaluateAgainst(String string) { @Override public void getTabCompleteOptions(String currentStart, Collection addTo) { - for (Enum type : options) { + for (final Enum type : options) { if (type.name().toLowerCase() .startsWith(currentStart.toLowerCase())) { addTo.add(type.name().toLowerCase()); @@ -49,8 +49,8 @@ public EnumNameBuilder(AICommandParameter annot, Class enumClass) { @Override public void addArguments(ArrayList list) { - list.add(new EnumArgumentDefinition(enumClass.getSimpleName(), annot.description(), - enumClass.getEnumConstants())); + list.add(new EnumArgumentDefinition(enumClass.getSimpleName(), annot + .description(), enumClass.getEnumConstants())); } @Override diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/FileNameBuilder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/FileNameBuilder.java index 5f79ab94..94cc826a 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/FileNameBuilder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/FileNameBuilder.java @@ -9,11 +9,11 @@ public class FileNameBuilder extends ParameterBuilder { public static boolean isFilenameValid(String file) { - File f = new File(file); + final File f = new File(file); try { f.getCanonicalPath(); return true; - } catch (IOException e) { + } catch (final IOException e) { return false; } } @@ -32,7 +32,7 @@ public boolean couldEvaluateAgainst(String string) { @Override public void getTabCompleteOptions(String currentStart, Collection addTo) { - for (File r : File.listRoots()) { + for (final File r : File.listRoots()) { if (r.getAbsolutePath().startsWith(currentStart)) { addTo.add(r.getAbsolutePath()); } @@ -47,9 +47,10 @@ public void getTabCompleteOptions(String currentStart, dir = dir.getParentFile(); } if (dir != null && dir.isDirectory()) { - for (File f : dir.listFiles()) { + for (final File f : dir.listFiles()) { if (f.getName().startsWith(namePrefix)) { - addTo.add(f.getPath() + (f.isDirectory() ? File.separator : "")); + addTo.add(f.getPath() + + (f.isDirectory() ? File.separator : "")); } } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/FixedNameBuilder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/FixedNameBuilder.java index a603cd07..7237b2ab 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/FixedNameBuilder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/FixedNameBuilder.java @@ -26,7 +26,7 @@ public void getTabCompleteOptions(String currentStart, public boolean couldEvaluateAgainst(String string) { return string.equals(fixedName); } - + public String getFixedName() { return fixedName; } @@ -41,7 +41,8 @@ public FixedNameBuilder(AICommandParameter annot) { @Override public void addArguments(ArrayList list) { - list.add(new FixedArgumentDefinition("'" + fixedName + "'", "fixed String")); + list.add(new FixedArgumentDefinition("'" + fixedName + "'", + "fixed String")); } @Override diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/IAIControllable.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/IAIControllable.java index 2fe178b1..287ac98e 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/IAIControllable.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/IAIControllable.java @@ -1,7 +1,7 @@ package net.famzangl.minecraft.minebot.ai.command; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.minecraft.client.Minecraft; public interface IAIControllable { diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/NumberNameBuilder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/NumberNameBuilder.java index 6c5441b1..cb79aa87 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/NumberNameBuilder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/NumberNameBuilder.java @@ -6,8 +6,9 @@ /** * A number. + * * @author michael - * + * */ public class NumberNameBuilder extends ParameterBuilder { diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/ParameterType.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/ParameterType.java index fb9d08a1..ec29bc27 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/ParameterType.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/ParameterType.java @@ -1,5 +1,12 @@ package net.famzangl.minecraft.minebot.ai.command; public enum ParameterType { - BLOCK_NAME, FIXED, NUMBER, COMMAND, COLOR, FILE, POSITION, ENUM + BLOCK_NAME, + FIXED, + NUMBER, + COMMAND, + COLOR, + FILE, + POSITION, + ENUM } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/PositionNameBuilder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/PositionNameBuilder.java index 0fedb628..e95f16e6 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/command/PositionNameBuilder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/command/PositionNameBuilder.java @@ -37,7 +37,7 @@ public void addArguments(ArrayList list) { @Override public Object getParameter(AIHelper helper, String[] arguments) { try { - ChunkCoordinates ppos = helper.getMinecraft().thePlayer + final ChunkCoordinates ppos = helper.getMinecraft().thePlayer .getPlayerCoordinates(); int i = ppos.posX; int j = ppos.posY - 2; @@ -49,7 +49,7 @@ public Object getParameter(AIHelper helper, String[] arguments) { k = MathHelper.floor_double(CommandBase.func_110666_a( helper.getMinecraft().thePlayer, k, arguments[2])); return new Pos(i, j, k); - } catch (NumberInvalidException e) { + } catch (final NumberInvalidException e) { throw new CommandEvaluationException("Number format not supported."); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandBuildWay.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandBuildWay.java index 0abf0106..6923914b 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandBuildWay.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandBuildWay.java @@ -2,12 +2,14 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.path.BuildWayPathfinder; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.BuildWayStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; import net.minecraftforge.common.util.ForgeDirection; @AICommand(helpText = "Build a nice way", name = "minebot") @@ -17,9 +19,10 @@ public class CommandBuildWay { public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "way", description = "") String nameArg) { - ForgeDirection dir = helper.getLookDirection(); - Pos pos = helper.getPlayerPosition(); - - return new BuildWayStrategy(dir, pos); + final ForgeDirection dir = helper.getLookDirection(); + final Pos pos = helper.getPlayerPosition(); + + return ValueActionStrategy.makeSafe(new BuildWayStrategy( + new BuildWayPathfinder(dir, pos))); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandEnchant.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandEnchant.java index 90e41787..6da7b8b7 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandEnchant.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandEnchant.java @@ -1,12 +1,13 @@ package net.famzangl.minecraft.minebot.ai.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; import net.famzangl.minecraft.minebot.ai.enchanting.EnchantStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; @AICommand(helpText = "Kill mobs in range and enchant whatever you have in the inventory.", name = "minebot") public class CommandEnchant { @@ -15,15 +16,15 @@ public class CommandEnchant { public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "enchant", description = "") String nameArg) { - return new EnchantStrategy(); + return ValueActionStrategy.makeSafe(new EnchantStrategy()); } @AICommandInvocation() public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "enchant", description = "") String nameArg, - @AICommandParameter(type = ParameterType.NUMBER, description = "") int level ) { - return new EnchantStrategy(level); + @AICommandParameter(type = ParameterType.NUMBER, description = "") int level) { + return ValueActionStrategy.makeSafe(new EnchantStrategy(level)); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandFeed.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandFeed.java index ae20afb8..bc0f273e 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandFeed.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandFeed.java @@ -1,12 +1,13 @@ package net.famzangl.minecraft.minebot.ai.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.FeedAnimalsStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; @AICommand(helpText = "Feeds animals in range\n" + "The animals can be restricted by color.", name = "minebot") @@ -16,14 +17,14 @@ public class CommandFeed { public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "feed", description = "") String nameArg) { - return new FeedAnimalsStrategy(); + return ValueActionStrategy.makeSafe(new FeedAnimalsStrategy()); } - + @AICommandInvocation() public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "feed", description = "") String nameArg, @AICommandParameter(type = ParameterType.COLOR, description = "The color of wolfes to feed.") int color) { - return new FeedAnimalsStrategy(color); + return ValueActionStrategy.makeSafe(new FeedAnimalsStrategy(color)); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandFish.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandFish.java index 668d81f9..21a78da2 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandFish.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandFish.java @@ -1,12 +1,13 @@ package net.famzangl.minecraft.minebot.ai.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.FishStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; @AICommand(helpText = "Catch some fish.", name = "minebot") public class CommandFish { @@ -15,6 +16,6 @@ public class CommandFish { public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "fish", description = "") String nameArg) { - return new FishStrategy(); + return ValueActionStrategy.makeSafe(new FishStrategy()); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandGetWood.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandGetWood.java index ce954bb2..6cea86fe 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandGetWood.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandGetWood.java @@ -1,13 +1,14 @@ package net.famzangl.minecraft.minebot.ai.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; import net.famzangl.minecraft.minebot.ai.path.TreePathFinder; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.PathFinderStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; import net.famzangl.minecraft.minebot.build.WoodType; @AICommand(helpText = "Gets wood", name = "minebot") @@ -16,8 +17,8 @@ public class CommandGetWood { public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "lumberjack", description = "") String nameArg) { - return new PathFinderStrategy(new TreePathFinder(helper, null), - "Getting some wood"); + return ValueActionStrategy.makeSafe(new PathFinderStrategy( + new TreePathFinder(null), "Getting some wood")); } @AICommandInvocation() @@ -25,8 +26,8 @@ public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "lumberjack", description = "") String nameArg, @AICommandParameter(type = ParameterType.ENUM, description = "wood type") WoodType type) { - return new PathFinderStrategy(new TreePathFinder(helper, type), - "Getting some wood"); + return ValueActionStrategy.makeSafe(new PathFinderStrategy( + new TreePathFinder(type), "Getting some wood")); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandHelp.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandHelp.java index 52d67265..c0865b15 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandHelp.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandHelp.java @@ -6,7 +6,6 @@ import java.util.List; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AIChatController; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; @@ -15,6 +14,7 @@ import net.famzangl.minecraft.minebot.ai.command.CommandDefinition; import net.famzangl.minecraft.minebot.ai.command.FixedNameBuilder.FixedArgumentDefinition; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.minecraft.client.entity.EntityClientPlayerMP; import net.minecraft.util.ChatComponentText; @@ -28,10 +28,10 @@ private static final class CommandToTextConverter implements Function { @Override public String apply(CommandDefinition command) { - StringBuilder stringBuilder = new StringBuilder(); + final StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("/"); stringBuilder.append(command.getCommandName()); - for (ArgumentDefinition arg : command.getArguments()) { + for (final ArgumentDefinition arg : command.getArguments()) { stringBuilder.append(" "); stringBuilder.append(arg.getDescriptionType()); } @@ -43,16 +43,18 @@ private static final class CommandComperator implements Comparator { @Override public int compare(CommandDefinition o1, CommandDefinition o2) { - int res = o1.getCommandName().compareTo(o2.getCommandName()); + final int res = o1.getCommandName().compareTo(o2.getCommandName()); if (res != 0) { return res; } else { - ArgumentDefinition a1 = o1.getArguments().get(0); - ArgumentDefinition a2 = o2.getArguments().get(0); + final ArgumentDefinition a1 = o1.getArguments().get(0); + final ArgumentDefinition a2 = o2.getArguments().get(0); if (a1 instanceof FixedArgumentDefinition && a2 instanceof FixedArgumentDefinition) { - String n1 = ((FixedArgumentDefinition) a1).getFixedName(); - String n2 = ((FixedArgumentDefinition) a2).getFixedName(); + final String n1 = ((FixedArgumentDefinition) a1) + .getFixedName(); + final String n2 = ((FixedArgumentDefinition) a2) + .getFixedName(); return n1.compareTo(n2); } else { return 0; @@ -73,7 +75,7 @@ public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "help", description = "") String nameArg, @AICommandParameter(type = ParameterType.NUMBER, description = "help page") int page) { - List commands = new ArrayList( + final List commands = new ArrayList( AIChatController.getRegistry().getAllCommands()); Collections.sort(commands, new CommandComperator()); AIChatController.addToChatPaged("Help", page, commands, @@ -88,10 +90,10 @@ public static AIStrategy run( @AICommandParameter(type = ParameterType.FIXED, fixedName = "help", description = "") String nameArg, @AICommandParameter(type = ParameterType.COMMAND, description = "command help") String commandName) { boolean found = false; - EntityClientPlayerMP player = helper.getMinecraft().thePlayer; - for (CommandDefinition command : AIChatController.getRegistry() + final EntityClientPlayerMP player = helper.getMinecraft().thePlayer; + for (final CommandDefinition command : AIChatController.getRegistry() .getAllCommands()) { - ArrayList args = command.getArguments(); + final ArrayList args = command.getArguments(); if (args.get(0).couldEvaluateAgainst(commandName)) { if (found) { player.addChatMessage(new ChatComponentText("")); @@ -109,19 +111,20 @@ public static AIStrategy run( private static void printHelp(EntityClientPlayerMP player, CommandDefinition command) { - CommandToTextConverter conv = new CommandToTextConverter(); + final CommandToTextConverter conv = new CommandToTextConverter(); - ChatComponentText headline = new ChatComponentText(conv.apply(command)); + final ChatComponentText headline = new ChatComponentText( + conv.apply(command)); headline.getChatStyle().setBold(true); player.addChatMessage(headline); - for (String line : command.getHelpText().split("\n")) { - ChatComponentText text = new ChatComponentText(line); + for (final String line : command.getHelpText().split("\n")) { + final ChatComponentText text = new ChatComponentText(line); text.getChatStyle().setItalic(true); player.addChatMessage(text); } - for (ArgumentDefinition arg : command.getArguments()) { - String[] help = arg.getDescriptionString().split("\n"); + for (final ArgumentDefinition arg : command.getArguments()) { + final String[] help = arg.getDescriptionString().split("\n"); for (final String text : help) { player.addChatMessage(new ChatComponentText(" " + text)); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandKill.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandKill.java index 3c51720f..17dd24a1 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandKill.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandKill.java @@ -1,13 +1,14 @@ package net.famzangl.minecraft.minebot.ai.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.animals.AnimalyType; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.KillAnimalsStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; @AICommand(helpText = "Starts hitting animals.\nA filter can be given.\nAvoids animals of your team.", name = "minebot") public class CommandKill { @@ -18,12 +19,12 @@ public static AIStrategy run( @AICommandParameter(type = ParameterType.FIXED, fixedName = "kill", description = "") String nameArg) { return run(helper, nameArg, AnimalyType.ANY); } - + @AICommandInvocation() public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "kill", description = "") String nameArg, @AICommandParameter(type = ParameterType.ENUM, description = "") AnimalyType type) { - return new KillAnimalsStrategy(0, type); + return ValueActionStrategy.makeSafe(new KillAnimalsStrategy(0, type)); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandMine.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandMine.java index 44863127..6df814c7 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandMine.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandMine.java @@ -1,11 +1,11 @@ package net.famzangl.minecraft.minebot.ai.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.MineStrategy; import net.minecraft.block.Block; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandPause.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandPause.java new file mode 100644 index 00000000..9bcc4fc1 --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandPause.java @@ -0,0 +1,20 @@ +package net.famzangl.minecraft.minebot.ai.commands; + +import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.command.AICommand; +import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; +import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; +import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.PauseStrategy; + +@AICommand(helpText = "Pause a given time (in seconds).", name = "minebot") +public class CommandPause { + @AICommandInvocation() + public static AIStrategy run( + AIHelper helper, + @AICommandParameter(type = ParameterType.FIXED, fixedName = "pause", description = "") String nameArg, + @AICommandParameter(type = ParameterType.NUMBER, description = "Time (in seconds)") int time) { + return new PauseStrategy(time); + } +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandPlant.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandPlant.java index 2faefe55..32c3407e 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandPlant.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandPlant.java @@ -1,30 +1,32 @@ package net.famzangl.minecraft.minebot.ai.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; import net.famzangl.minecraft.minebot.ai.path.PlantPathFinder; import net.famzangl.minecraft.minebot.ai.path.PlantPathFinder.PlantType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.PathFinderStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; -@AICommand(helpText = "Plants plants\n" - + "Uses a hoe if needed.", name = "minebot") +@AICommand(helpText = "Plants plants\n" + "Uses a hoe if needed.", name = "minebot") public class CommandPlant { @AICommandInvocation() public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "plant", description = "") String nameArg) { - return new PathFinderStrategy(new PlantPathFinder(helper, PlantType.ANY), "Planting"); + return ValueActionStrategy.makeSafe(new PathFinderStrategy( + new PlantPathFinder(PlantType.ANY), "Planting")); } - + @AICommandInvocation() public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "plant", description = "") String nameArg, @AICommandParameter(type = ParameterType.ENUM, description = "plant type") PlantType type) { - return new PathFinderStrategy(new PlantPathFinder(helper, type), "Planting"); + return ValueActionStrategy.makeSafe(new PathFinderStrategy( + new PlantPathFinder(type), "Planting")); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandRun.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandRun.java index e3f4d938..7ad7a3df 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandRun.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandRun.java @@ -1,34 +1,83 @@ package net.famzangl.minecraft.minebot.ai.commands; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; -import java.util.ArrayList; +import java.util.LinkedList; import java.util.Scanner; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AIChatController; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; +import net.famzangl.minecraft.minebot.ai.command.IAIControllable; import net.famzangl.minecraft.minebot.ai.command.ParameterType; -import net.famzangl.minecraft.minebot.ai.task.AITask; -import net.famzangl.minecraft.minebot.ai.task.SendCommandTask; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiChat; +import net.minecraft.client.gui.GuiScreen; @AICommand(helpText = "Run commands from a file.", name = "minebot") public class CommandRun { + private static final class StrategyReceiver implements IAIControllable { + private final IAIControllable controlled; + private AIStrategy receivedStrategy; - private static final class RundFileStrategy implements AIStrategy { - private boolean added; - private final String fileName; + private StrategyReceiver(IAIControllable controlled) { + this.controlled = controlled; + } @Override - public void searchTasks(AIHelper helper) { - if (!added) { - added = true; - final File file = new File(fileName); - try { - final ArrayList commands = new ArrayList(); + public void requestUseStrategy(AIStrategy strategy) { + receivedStrategy = strategy; + } + + @Override + public Minecraft getMinecraft() { + return controlled.getMinecraft(); + } + + @Override + public AIHelper getAiHelper() { + return controlled.getAiHelper(); + } + + public AIStrategy getReceivedStrategy() { + return receivedStrategy; + } + } + + private static final class RunFileStrategy extends AIStrategy { + + private final String fileName; + private LinkedList commands = null; + private IOException exception = null; + private boolean fileRead; + private boolean exceptionReported; + private AIStrategy activeStrategy; + private boolean repeatMode = false; + + public RunFileStrategy(String fileName) { + super(); + this.fileName = fileName; + new Thread() { + @Override + public void run() { + try { + commands = readFile(new File( + RunFileStrategy.this.fileName)); + } catch (final FileNotFoundException e) { + e.printStackTrace(); + exception = e; + } finally { + fileRead = true; + } + }; + + private LinkedList readFile(final File file) + throws FileNotFoundException { + final LinkedList commands = new LinkedList(); final Scanner reader = new Scanner(file); String line; while (reader.hasNextLine()) { @@ -38,29 +87,87 @@ public void searchTasks(AIHelper helper) { } } reader.close(); - helper.addTask(new SendCommandTask(commands)); - } catch (final IOException e) { - AIChatController - .addChatLine("Error while reading file: " - + e.getMessage()); - e.printStackTrace(); + return commands; } - } + }.start(); + } + + @Override + public boolean checkShouldTakeOver(AIHelper helper) { + return !fileRead + || (exception != null ? !exceptionReported : !commands + .isEmpty()); } @Override - public AITask getOverrideTask(AIHelper helper) { - return null; + protected TickResult onGameTick(AIHelper helper) { + if (!fileRead) { + return TickResult.TICK_HANDLED; + } else if (exception != null) { + if (!exceptionReported) { + AIChatController.addChatLine("Error while reading file: " + + exception.getMessage()); + exceptionReported = true; + } + return TickResult.NO_MORE_WORK; + } else if (activeStrategy != null) { + final TickResult result = activeStrategy.gameTick(helper); + if (result == TickResult.NO_MORE_WORK) { + activeStrategy.setActive(false, helper); + activeStrategy = null; + return TickResult.TICK_AGAIN; + } else { + return result; + } + } else if (commands.isEmpty()) { + + AIChatController.addChatLine("Done"); + return TickResult.NO_MORE_WORK; + } else { + final IAIControllable controlled = AIChatController + .getRegistry().getControlled(); + final StrategyReceiver tempController = new StrategyReceiver( + controlled); + try { + AIChatController.getRegistry() + .setControlled(tempController); + final String command = commands.removeFirst(); + runCommand(helper, command); + if (repeatMode) { + commands.add(command); + } + } finally { + AIChatController.getRegistry().setControlled(controlled); + } + if (tempController.receivedStrategy != null) { + activeStrategy = tempController.getReceivedStrategy(); + activeStrategy.setActive(true, helper); + } + return TickResult.TICK_AGAIN; + } + } + + private void runCommand(AIHelper helper, String command) { + if (command.equals("repeat:")) { + repeatMode = true; + } else { + CommandRun.runCommand(helper, command); + } } @Override public String getDescription() { - return "Loading"; + return "Running from file."; } - public RundFileStrategy(String fileName) { - super(); - this.fileName = fileName; + } + + public static void runCommand(AIHelper helper, String command) { + if (helper.getMinecraft().ingameGUI.getChatGUI() != null) { + final GuiChat chat = new GuiChat(); + helper.getMinecraft().displayGuiScreen(chat); + chat.func_146403_a(command); + helper.getMinecraft().displayGuiScreen((GuiScreen) null); } } @@ -69,6 +176,6 @@ public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "run", description = "") String nameArg, @AICommandParameter(type = ParameterType.FILE, description = "") String file) { - return new RundFileStrategy(file); + return new RunFileStrategy(file); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandShear.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandShear.java index 79c2690c..5d5e1669 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandShear.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandShear.java @@ -1,14 +1,15 @@ package net.famzangl.minecraft.minebot.ai.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.ShearStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; -@AICommand(helpText="Shear sheep.", name="minebot") +@AICommand(helpText = "Shear sheep.", name = "minebot") public class CommandShear { @AICommandInvocation() @@ -17,13 +18,13 @@ public static AIStrategy run( @AICommandParameter(type = ParameterType.FIXED, fixedName = "shear", description = "") String nameArg) { return run(helper, nameArg, -1); } - + @AICommandInvocation() public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "shear", description = "") String nameArg, @AICommandParameter(type = ParameterType.COLOR, description = "The color to get") int color) { - return new ShearStrategy(color); + return ValueActionStrategy.makeSafe(new ShearStrategy(color)); } - + } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandSit.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandSit.java index a955b978..7c019919 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandSit.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandSit.java @@ -1,15 +1,16 @@ package net.famzangl.minecraft.minebot.ai.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.animals.AnimalyType; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.LetAnimalsSitStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; -@AICommand(helpText="Lets all dogs either sit or stand.", name="minebot") +@AICommand(helpText = "Lets all dogs either sit or stand.", name = "minebot") public class CommandSit { @AICommandInvocation() @@ -24,8 +25,10 @@ public static AIStrategy runSit( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "sit", description = "") String nameArg, @AICommandParameter(type = ParameterType.COLOR, description = "The color of wolfes to feed.") int color) { - return new LetAnimalsSitStrategy(AnimalyType.WOLF, true, color); + return ValueActionStrategy.makeSafe(new LetAnimalsSitStrategy( + AnimalyType.WOLF, true, color)); } + @AICommandInvocation() public static AIStrategy runUnSit( AIHelper helper, @@ -38,6 +41,7 @@ public static AIStrategy runUnSit( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "unsit", description = "") String nameArg, @AICommandParameter(type = ParameterType.COLOR, description = "The color of wolfes to feed.") int color) { - return new LetAnimalsSitStrategy(AnimalyType.WOLF, false, color); + return ValueActionStrategy.makeSafe(new LetAnimalsSitStrategy( + AnimalyType.WOLF, false, color)); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandStop.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandStop.java index 5dfc769f..39144d3a 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandStop.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandStop.java @@ -1,11 +1,11 @@ package net.famzangl.minecraft.minebot.ai.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.StopStrategy; @AICommand(name = "minebot", helpText = "Stop whatever you are doing.") diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandTint.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandTint.java index 5cb7c26b..79d0ba4b 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandTint.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandTint.java @@ -1,15 +1,16 @@ package net.famzangl.minecraft.minebot.ai.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.TintStrategy; import net.famzangl.minecraft.minebot.ai.strategy.TintStrategy.TintType; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; -@AICommand(helpText="Tints wolves and sheep with the given color", name="minebot") +@AICommand(helpText = "Tints wolves and sheep with the given color", name = "minebot") public class CommandTint { @AICommandInvocation() @@ -18,7 +19,7 @@ public static AIStrategy run( @AICommandParameter(type = ParameterType.FIXED, fixedName = "tint", description = "") String nameArg) { return run(helper, nameArg, -1, TintType.ANY); } - + @AICommandInvocation() public static AIStrategy run( AIHelper helper, @@ -26,7 +27,7 @@ public static AIStrategy run( @AICommandParameter(type = ParameterType.COLOR, description = "The color to use") int color) { return run(helper, nameArg, color, TintType.ANY); } - + @AICommandInvocation() public static AIStrategy run( AIHelper helper, @@ -35,7 +36,7 @@ public static AIStrategy run( @AICommandParameter(type = ParameterType.ENUM, description = "Animals to apply the tint to") TintType type) { return run(helper, nameArg, color, type, -1); } - + @AICommandInvocation() public static AIStrategy run( AIHelper helper, @@ -43,6 +44,7 @@ public static AIStrategy run( @AICommandParameter(type = ParameterType.COLOR, description = "The color to use") int color, @AICommandParameter(type = ParameterType.ENUM, description = "Animals to apply the tint to") TintType type, @AICommandParameter(type = ParameterType.COLOR, description = "The color the wolf should currently have") int current) { - return new TintStrategy(color, type, current); + return ValueActionStrategy.makeSafe(new TintStrategy(color, type, + current)); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandTunnel.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandTunnel.java index 70c92631..0a047a32 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandTunnel.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandTunnel.java @@ -2,14 +2,15 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; import net.famzangl.minecraft.minebot.ai.path.TunnelPathFinder; import net.famzangl.minecraft.minebot.ai.path.TunnelPathFinder.TorchSide; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.PathFinderStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; import net.minecraftforge.common.util.ForgeDirection; @AICommand(helpText = "Build a tunnel with the given profile", name = "minebot") @@ -28,14 +29,15 @@ public static AIStrategy run( @AICommandParameter(type = ParameterType.ENUM, description = "direction") ForgeDirection inDirection) { return run(helper, nameArg, inDirection, 0, 0); } - + @AICommandInvocation() public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "tunnel", description = "") String nameArg, @AICommandParameter(type = ParameterType.NUMBER, description = "add to side") int addToSide, @AICommandParameter(type = ParameterType.NUMBER, description = "add to top") int addToTop) { - return run(helper, nameArg, helper.getLookDirection(), addToSide, addToTop); + return run(helper, nameArg, helper.getLookDirection(), addToSide, + addToTop); } @AICommandInvocation() @@ -45,10 +47,10 @@ public static AIStrategy run( @AICommandParameter(type = ParameterType.NUMBER, description = "add to side") int addToSide, @AICommandParameter(type = ParameterType.NUMBER, description = "add to top") int addToTop, @AICommandParameter(type = ParameterType.ENUM, description = "torche side") TorchSide torches) { - return run(helper, nameArg, helper.getLookDirection(), addToSide, addToTop, torches); + return run(helper, nameArg, helper.getLookDirection(), addToSide, + addToTop, torches); } - @AICommandInvocation() public static AIStrategy run( AIHelper helper, @@ -56,7 +58,8 @@ public static AIStrategy run( @AICommandParameter(type = ParameterType.ENUM, description = "direction") ForgeDirection inDirection, @AICommandParameter(type = ParameterType.NUMBER, description = "add to side") int addToSide, @AICommandParameter(type = ParameterType.NUMBER, description = "add to top") int addToTop) { - return run(helper, inDirection.offsetX, inDirection.offsetZ, addToSide, addToTop, TorchSide.NONE); + return run(helper, inDirection.offsetX, inDirection.offsetZ, addToSide, + addToTop, TorchSide.NONE); } @AICommandInvocation() @@ -67,14 +70,15 @@ public static AIStrategy run( @AICommandParameter(type = ParameterType.NUMBER, description = "add to side") int addToSide, @AICommandParameter(type = ParameterType.NUMBER, description = "add to top") int addToTop, @AICommandParameter(type = ParameterType.ENUM, description = "torche side") TorchSide torches) { - return run(helper, inDirection.offsetX, inDirection.offsetZ, addToSide, addToTop, torches); + return run(helper, inDirection.offsetX, inDirection.offsetZ, addToSide, + addToTop, torches); } - public static AIStrategy run( - AIHelper helper, - int dx, int dz, int addToSide, int addToTop, TorchSide torches) { - Pos pos = helper.getPlayerPosition(); - return new PathFinderStrategy(new TunnelPathFinder(helper, dx, dz, pos.x, pos.y, pos.z, addToSide, addToTop, torches), - "Tunneling"); + public static AIStrategy run(AIHelper helper, int dx, int dz, + int addToSide, int addToTop, TorchSide torches) { + final Pos pos = helper.getPlayerPosition(); + return ValueActionStrategy.makeSafe(new PathFinderStrategy( + new TunnelPathFinder(dx, dz, pos.x, pos.y, pos.z, addToSide, + addToTop, torches), "Tunneling")); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandUngrab.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandUngrab.java index dca09ad4..0c2b40d4 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandUngrab.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/commands/CommandUngrab.java @@ -1,15 +1,15 @@ package net.famzangl.minecraft.minebot.ai.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; -@AICommand(helpText = "Ungrabs the mouse cursor\n" + - "The bot can then still run while you do other things on your computer.\n" + - "Just click in the window to re-grab the mouse.", name = "minebot") +@AICommand(helpText = "Ungrabs the mouse cursor\n" + + "The bot can then still run while you do other things on your computer.\n" + + "Just click in the window to re-grab the mouse.", name = "minebot") public class CommandUngrab { @AICommandInvocation() diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/CloseScreenTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/CloseScreenTask.java index 625e0c57..f23ee919 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/CloseScreenTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/CloseScreenTask.java @@ -1,6 +1,7 @@ package net.famzangl.minecraft.minebot.ai.enchanting; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; import net.minecraft.client.gui.GuiScreen; @@ -12,7 +13,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { h.getMinecraft().displayGuiScreen((GuiScreen) null); h.getMinecraft().setIngameFocus(); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/EnchantStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/EnchantStrategy.java index f406ac95..0f674598 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/EnchantStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/EnchantStrategy.java @@ -1,9 +1,7 @@ package net.famzangl.minecraft.minebot.ai.enchanting; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; -import net.famzangl.minecraft.minebot.ai.AIStrategyFactory; -import net.famzangl.minecraft.minebot.ai.task.AITask; +import net.famzangl.minecraft.minebot.ai.strategy.TaskStrategy; import net.minecraft.client.gui.GuiEnchantment; /** @@ -13,40 +11,34 @@ * */ -public class EnchantStrategy implements AIStrategy, AIStrategyFactory { - +public class EnchantStrategy extends TaskStrategy { private final int level; public EnchantStrategy() { this(30); } - + public EnchantStrategy(int level) { this.level = level; } - @Override - public AIStrategy produceStrategy(AIHelper helper) { - return this; - } - @Override public void searchTasks(AIHelper helper) { if (hasLevelsToEnchant(helper)) { if (enchantmentTableOpened(helper)) { - helper.addTask(new PutItemInTableTask()); - helper.addTask(new SelectEnchantmentTask()); + addTask(new PutItemInTableTask()); + addTask(new SelectEnchantmentTask()); } else { - helper.addTask(new FaceBlockOfTypeTask()); - helper.addTask(new ClickOnEnchantmentTable()); + addTask(new FaceBlockOfTypeTask()); + addTask(new ClickOnEnchantmentTable()); } } else if (enchantmentTableOpened(helper)) { - helper.addTask(new TakeEnchantedItemTask()); - helper.addTask(new CloseScreenTask()); + addTask(new TakeEnchantedItemTask()); + addTask(new CloseScreenTask()); } else { - helper.addTask(new FaceAnyMobTask()); - helper.addTask(new KillAnyMobTask()); + addTask(new FaceAnyMobTask()); + addTask(new KillAnyMobTask()); } } @@ -62,9 +54,4 @@ private boolean enchantmentTableOpened(AIHelper helper) { public String getDescription() { return "Enchanting for level " + level; } - - @Override - public AITask getOverrideTask(AIHelper helper) { - return null; - } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/FaceAnyMobTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/FaceAnyMobTask.java index cf1361fb..d865d261 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/FaceAnyMobTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/FaceAnyMobTask.java @@ -4,6 +4,7 @@ import java.util.Random; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; import net.minecraft.client.Minecraft; import net.minecraft.command.IEntitySelector; @@ -12,8 +13,7 @@ import net.minecraft.util.AxisAlignedBB; public class FaceAnyMobTask extends AITask { - private final class LivingSelector implements - IEntitySelector { + private final class LivingSelector implements IEntitySelector { @Override public boolean isEntityApplicable(Entity var1) { // TODO: better filter @@ -30,7 +30,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { tickCount++; if (tickCount > 10) { final Minecraft mc = h.getMinecraft(); diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/FaceBlockOfTypeTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/FaceBlockOfTypeTask.java index 4d056218..20a75d98 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/FaceBlockOfTypeTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/FaceBlockOfTypeTask.java @@ -2,6 +2,7 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; import net.minecraft.init.Blocks; @@ -14,7 +15,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { final Pos pos = h.findBlock(Blocks.enchanting_table); if (pos == null) { System.out.println("Could not find block around player."); diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/KillAnyMobTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/KillAnyMobTask.java index ff0f11fa..0a8d483b 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/KillAnyMobTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/KillAnyMobTask.java @@ -1,6 +1,7 @@ package net.famzangl.minecraft.minebot.ai.enchanting; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; import net.minecraft.util.MovingObjectPosition; @@ -16,7 +17,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { tickCount++; if (tickCount % 10 == 5) { h.overrideAttack(); diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/PutItemInTableTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/PutItemInTableTask.java index 43002697..8e5285ca 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/PutItemInTableTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/PutItemInTableTask.java @@ -1,7 +1,9 @@ package net.famzangl.minecraft.minebot.ai.enchanting; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; +import net.famzangl.minecraft.minebot.ai.task.error.StringTaskError; import net.minecraft.client.gui.GuiEnchantment; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; @@ -15,7 +17,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { final GuiEnchantment screen = (GuiEnchantment) h.getMinecraft().currentScreen; for (int i = 1; i < 9 * 4 + 1; i++) { final Slot slot = screen.inventorySlots.getSlot(i); @@ -36,7 +38,7 @@ public void runTick(AIHelper h) { } } System.out.println("No item to put."); - h.desync(); + o.desync(new StringTaskError("No item for table.")); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/SelectEnchantmentTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/SelectEnchantmentTask.java index 1b008c31..6d3417a6 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/SelectEnchantmentTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/SelectEnchantmentTask.java @@ -3,7 +3,9 @@ import java.lang.reflect.Field; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; +import net.famzangl.minecraft.minebot.ai.task.error.StringTaskError; import net.minecraft.client.gui.GuiEnchantment; import net.minecraft.inventory.ContainerEnchantment; @@ -24,16 +26,16 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { if (!(h.getMinecraft().currentScreen instanceof GuiEnchantment)) { System.out.println("Screen not opened."); - h.desync(); + o.desync(new StringTaskError("Enchantment screen is not open.")); return; } final GuiEnchantment screen = (GuiEnchantment) h.getMinecraft().currentScreen; if (!screen.inventorySlots.getSlot(0).getHasStack()) { System.out.println("No stack in slot."); - h.desync(); + o.desync(new StringTaskError("No stack in enchantment table.")); return; } if (screen.inventorySlots.getSlot(0).getStack().isItemEnchanted()) { @@ -64,7 +66,7 @@ public void runTick(AIHelper h) { return; } catch (final Throwable e) { e.printStackTrace(); - h.desync(); + o.desync(new StringTaskError("Some error... :-(")); return; } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/TakeEnchantedItemTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/TakeEnchantedItemTask.java index 2a753bd5..74ab8055 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/TakeEnchantedItemTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/enchanting/TakeEnchantedItemTask.java @@ -1,6 +1,7 @@ package net.famzangl.minecraft.minebot.ai.enchanting; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; import net.minecraft.client.gui.GuiEnchantment; @@ -16,7 +17,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { if (!(h.getMinecraft().currentScreen instanceof GuiEnchantment)) { System.out.println("Screen not opened."); return; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/AlongTrackPathFinder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/AlongTrackPathFinder.java index 08f0f85f..c60a6a20 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/AlongTrackPathFinder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/AlongTrackPathFinder.java @@ -1,7 +1,5 @@ package net.famzangl.minecraft.minebot.ai.path; -import net.famzangl.minecraft.minebot.ai.AIHelper; - public class AlongTrackPathFinder extends MovePathFinder { protected final int dx; protected final int dz; @@ -9,9 +7,7 @@ public class AlongTrackPathFinder extends MovePathFinder { protected final int cy; protected final int cz; - public AlongTrackPathFinder(AIHelper helper, int dx, int dz, int cx, - int cy, int cz) { - super(helper); + public AlongTrackPathFinder(int dx, int dz, int cx, int cy, int cz) { this.dx = dx; this.dz = dz; this.cx = cx; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/BuildWayPathfinder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/BuildWayPathfinder.java index 9c35377c..e0761014 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/BuildWayPathfinder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/BuildWayPathfinder.java @@ -54,9 +54,12 @@ public class BuildWayPathfinder extends AlongTrackPathFinder { private static final Block BRIDGE_WALL = Blocks.cobblestone_wall; private final int width = 2; - public BuildWayPathfinder(AIHelper helper, int dx, int dz, int cx, int cy, - int cz) { - super(helper, dx, dz, cx, cy, cz); + public BuildWayPathfinder(int dx, int dz, int cx, int cy, int cz) { + super(dx, dz, cx, cy, cz); + } + + public BuildWayPathfinder(ForgeDirection dir, Pos pos) { + this(dir.offsetX, dir.offsetZ, pos.x, pos.y, pos.z); } /** @@ -92,44 +95,42 @@ public boolean isDone() { * @param currentPos */ public void addConstructionTasks(Pos currentPos) { - helper.addTask(new GetOnHotBarTask(new SlabFilter(FLOOR))); + addTask(new GetOnHotBarTask(new SlabFilter(FLOOR))); if (placeTorch) { - helper.addTask(new GetOnHotBarTask(new BlockItemFilter(Blocks.torch))); + addTask(new GetOnHotBarTask(new BlockItemFilter(Blocks.torch))); } - - Pos first = getPos(0, -1); - AITask placeTask = new BuildHalfslabTask(first, FLOOR, + + final Pos first = getPos(0, -1); + final AITask placeTask = new BuildHalfslabTask(first, FLOOR, BlockSide.LOWER_HALF).getPlaceBlockTask(currentPos .subtract(first)); - helper.addTask(placeTask); + addTask(placeTask); - DestroyInRangeTask clearTask = getClearAreaTask(); + final DestroyInRangeTask clearTask = getClearAreaTask(); clearTask.blacklist(first); - helper.addTask(clearTask); + addTask(clearTask); for (int i = 1; i < width; i++) { - Pos current = getPos(i, -1); - helper.addTask(new BuildHalfslabTask(current, FLOOR, + final Pos current = getPos(i, -1); + addTask(new BuildHalfslabTask(current, FLOOR, BlockSide.LOWER_HALF) .getPlaceBlockTask(getPos(i - 1, 0).subtract(current))); } addFarSideBuildTasks(); - helper.addTask(new WalkTowardsTask(getPos(width - 1, 0), getPos(0, - 0))); + addTask(new WalkTowardsTask(getPos(width - 1, 0), getPos(0, 0))); addNearSideBuildTasks(); done = true; } protected void addFarSideBuildTasks() { - helper.addTask(new WalkTowardsTask(getPos(width, 1), getPos( - width - 1, 0))); + addTask(new WalkTowardsTask(getPos(width, 1), getPos(width - 1, 0))); } protected void addNearSideBuildTasks() { - helper.addTask(new WalkTowardsTask(getPos(-1, 1), getPos(0, 0))); + addTask(new WalkTowardsTask(getPos(-1, 1), getPos(0, 0))); } protected DestroyInRangeTask getClearAreaTask() { @@ -137,15 +138,15 @@ protected DestroyInRangeTask getClearAreaTask() { } public void addConstructionTasksFromInner() { - helper.addTask(new DestroyInRangeTask(getPos(0, -1), getPos(0, 1))); + addTask(new DestroyInRangeTask(getPos(0, -1), getPos(0, 1))); addConstructionTasks(getPos(0, -1)); } public void addConstructionTasksFromPrevoius(Pos current) { - Pos floorPos = getPos(0, -1); - Block floor = helper.getBlock(floorPos); - helper.addTask(new DestroyInRangeTask(floorPos, getPos(0, 1))); + final Pos floorPos = getPos(0, -1); + final Block floor = helper.getBlock(floorPos); + addTask(new DestroyInRangeTask(floorPos, getPos(0, 1))); // helper.addTask(new SneakAndPlaceAtHalfTask(floorPos.x, // floorPos.y, // floorPos.z, FLOOR, current, floorPos.y + .5, @@ -155,16 +156,16 @@ public void addConstructionTasksFromPrevoius(Pos current) { } protected final void addNearSide() { - CubeBuildTask task = new BlockBuildTask(getPos(-1, -1), BRIDGE_SIDE); - helper.addTask(task.getPlaceBlockTask(getPos(1, 1).subtract( - getPos(0, 0)))); + final CubeBuildTask task = new BlockBuildTask(getPos(-1, -1), + BRIDGE_SIDE); + addTask(task.getPlaceBlockTask(getPos(1, 1).subtract(getPos(0, 0)))); } protected final void addFarSide() { - CubeBuildTask task = new BlockBuildTask(getPos(width, -1), + final CubeBuildTask task = new BlockBuildTask(getPos(width, -1), BRIDGE_SIDE); - helper.addTask(task.getPlaceBlockTask(getPos(-1, 1).subtract( - getPos(0, 0)))); + addTask(task + .getPlaceBlockTask(getPos(-1, 1).subtract(getPos(0, 0)))); } } @@ -177,7 +178,7 @@ public boolean needsTunnel() { int covered = 0; for (int y = 2; y <= 3; y++) { for (int u = -1; u <= width + 1; u++) { - Block block = helper.getBlock(getPos(u, y)); + final Block block = helper.getBlock(getPos(u, y)); if (AIHelper.blockIsOneOf(block, AIHelper.normalBlocks) || AIHelper.blockIsOneOf(block, AIHelper.fallingBlocks)) { @@ -190,13 +191,13 @@ public boolean needsTunnel() { } public boolean needsBridge() { - return (isAirlike(-1, 0) && isAirlike(-1, -1) && isAirlike(-1, -2)) - || (isAirlike(width, 0) && isAirlike(width, -1) && isAirlike( - width, -2)); + return isAirlike(-1, 0) && isAirlike(-1, -1) && isAirlike(-1, -2) + || isAirlike(width, 0) && isAirlike(width, -1) + && isAirlike(width, -2); } private boolean isAirlike(int u, int dy) { - Block block = helper.getBlock(getPos(u, dy)); + final Block block = helper.getBlock(getPos(u, dy)); return AIHelper.blockIsOneOf(block, Blocks.air) || AIHelper.blockIsOneOf(block, AIHelper.walkableBlocks); } @@ -207,11 +208,11 @@ private class BridgeWayType extends WayPiece { public BridgeWayType(int stepIndex) { super(stepIndex); } - + @Override public void addConstructionTasks(Pos currentPos) { - helper.addTask(new GetOnHotBarTask(new BlockItemFilter(BRIDGE_WALL))); - helper.addTask(new GetOnHotBarTask(new BlockItemFilter(BRIDGE_SIDE))); + addTask(new GetOnHotBarTask(new BlockItemFilter(BRIDGE_WALL))); + addTask(new GetOnHotBarTask(new BlockItemFilter(BRIDGE_SIDE))); super.addConstructionTasks(currentPos); } @@ -219,11 +220,12 @@ public void addConstructionTasks(Pos currentPos) { protected void addFarSideBuildTasks() { addFarSide(); super.addFarSideBuildTasks(); - helper.addTask(new PlaceBlockTask(getPos(width, -1), - ForgeDirection.UP, BRIDGE_WALL)); + addTask(new PlaceBlockTask(getPos(width, -1), ForgeDirection.UP, + BRIDGE_WALL)); if (placeTorch) { - helper.addTask(new PlaceTorchSomewhereTask(Collections - .singletonList(getPos(width, 1)), ForgeDirection.DOWN)); + addTask(new PlaceTorchSomewhereTask( + Collections.singletonList(getPos(width, 1)), + ForgeDirection.DOWN)); } } @@ -231,11 +233,12 @@ protected void addFarSideBuildTasks() { protected void addNearSideBuildTasks() { addNearSide(); super.addNearSideBuildTasks(); - helper.addTask(new PlaceBlockTask(getPos(-1, -1), - ForgeDirection.UP, BRIDGE_WALL)); + addTask(new PlaceBlockTask(getPos(-1, -1), ForgeDirection.UP, + BRIDGE_WALL)); if (placeTorch) { - helper.addTask(new PlaceTorchSomewhereTask(Collections - .singletonList(getPos(-1, 1)), ForgeDirection.DOWN)); + addTask(new PlaceTorchSomewhereTask( + Collections.singletonList(getPos(-1, 1)), + ForgeDirection.DOWN)); } } @@ -256,10 +259,9 @@ public TunnelWayType(int stepIndex) { protected void addNearSideBuildTasks() { super.addNearSideBuildTasks(); if (placeTorch) { - helper.addTask(new PlaceTorchSomewhereTask(Arrays.asList( - getPos(0, 1), getPos(0, 0), getPos(-1, 1)), - getForwardDirection().getRotation(ForgeDirection.UP), - ForgeDirection.DOWN)); + addTask(new PlaceTorchSomewhereTask(Arrays.asList(getPos(0, 1), + getPos(0, 0), getPos(-1, 1)), getForwardDirection() + .getRotation(ForgeDirection.UP), ForgeDirection.DOWN)); } } @@ -267,7 +269,7 @@ protected void addNearSideBuildTasks() { protected void addFarSideBuildTasks() { super.addFarSideBuildTasks(); if (placeTorch) { - helper.addTask(new PlaceTorchSomewhereTask(Arrays.asList( + addTask(new PlaceTorchSomewhereTask(Arrays.asList( getPos(width - 1, 1), getPos(width - 1, 0), getPos(width, 1)), getForwardDirection().getRotation( ForgeDirection.DOWN), ForgeDirection.DOWN)); @@ -280,11 +282,11 @@ private class FlatlandWayType extends WayPiece { public FlatlandWayType(int stepIndex) { super(stepIndex); } - + @Override public void addConstructionTasks(Pos currentPos) { if (placeTorch) { - helper.addTask(new GetOnHotBarTask(new BlockItemFilter(BRIDGE_SIDE))); + addTask(new GetOnHotBarTask(new BlockItemFilter(BRIDGE_SIDE))); } super.addConstructionTasks(currentPos); } @@ -305,8 +307,9 @@ protected void addFarSideBuildTasks() { } super.addFarSideBuildTasks(); if (placeTorch) { - helper.addTask(new PlaceTorchSomewhereTask(Collections - .singletonList(getPos(width, 0)), ForgeDirection.DOWN)); + addTask(new PlaceTorchSomewhereTask( + Collections.singletonList(getPos(width, 0)), + ForgeDirection.DOWN)); } } @@ -317,8 +320,9 @@ protected void addNearSideBuildTasks() { } super.addNearSideBuildTasks(); if (placeTorch) { - helper.addTask(new PlaceTorchSomewhereTask(Collections - .singletonList(getPos(-1, 0)), ForgeDirection.DOWN)); + addTask(new PlaceTorchSomewhereTask( + Collections.singletonList(getPos(-1, 0)), + ForgeDirection.DOWN)); } } } @@ -337,8 +341,8 @@ public ForgeDirection getForwardDirection() { } private void computeNextWayType() { - int i = wayTypes.size(); - NormalWayType base = new NormalWayType(i); + final int i = wayTypes.size(); + final NormalWayType base = new NormalWayType(i); WayPiece type = base; if (base.needsBridge()) { type = new BridgeWayType(i); @@ -354,7 +358,7 @@ private void computeNextWayType() { @Override protected void addTasksForTarget(Pos currentPos) { - int currentStep = getStepNumber(currentPos.x, currentPos.z); + final int currentStep = getStepNumber(currentPos.x, currentPos.z); if (currentPos.y == cy - 1 && !getSuggestedWayType(currentStep).isDone()) { getSuggestedWayType(currentStep).addConstructionTasksFromInner(); @@ -375,8 +379,9 @@ public boolean addContinuingTask(Pos playerPosition) { && playerPosition.y == cy - 1 && AIHelper.blockIsOneOf(helper.getBlock(playerPosition), FLOOR.slabBlock)) { - int currentStep = getStepNumber(playerPosition.x, playerPosition.z); - WayPiece next = getSuggestedWayType(currentStep + 1); + final int currentStep = getStepNumber(playerPosition.x, + playerPosition.z); + final WayPiece next = getSuggestedWayType(currentStep + 1); if (!next.isDone()) { next.addConstructionTasksFromPrevoius(playerPosition.add(0, 1, 0)); @@ -391,8 +396,8 @@ protected float rateDestination(int distance, int x, int y, int z) { if (!isOnTrack(x, z)) { return -1; } - int currentStep = getStepNumber(x, z); - if ((y == cy - 1 && !getSuggestedWayType(currentStep).isDone())) { + final int currentStep = getStepNumber(x, z); + if (y == cy - 1 && !getSuggestedWayType(currentStep).isDone()) { return distance; } else { return -1; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/ClearAreaPathfinder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/ClearAreaPathfinder.java index 46370b97..2ff2faea 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/ClearAreaPathfinder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/ClearAreaPathfinder.java @@ -16,20 +16,18 @@ public class ClearAreaPathfinder extends MovePathFinder { private final HashSet foundPositions = new HashSet(); private Pos pathEndPosition; - public ClearAreaPathfinder(AIHelper helper) { - super(helper); - minPos = Pos.minPos(helper.getPos1(), helper.getPos2()); - maxPos = Pos.maxPos(helper.getPos1(), helper.getPos2()); + public ClearAreaPathfinder(Pos pos1, Pos pos2) { + minPos = Pos.minPos(pos1, pos2); + maxPos = Pos.maxPos(pos1, pos2); topY = maxPos.y; } @Override - public boolean searchSomethingAround(int cx, int cy, int cz) { + protected boolean runSearch(Pos playerPosition) { foundPositions.clear(); - pathEndPosition = new Pos(cx, cy, cz); + pathEndPosition = playerPosition; do { - boolean finished = super.searchSomethingAround(pathEndPosition.x, - pathEndPosition.y, pathEndPosition.z); + final boolean finished = super.runSearch(pathEndPosition); if (!finished) { return false; } @@ -39,7 +37,7 @@ public boolean searchSomethingAround(int cx, int cy, int cz) { @Override protected void foundPath(LinkedList path) { - for (Pos p : path) { + for (final Pos p : path) { foundPositions.add(p); foundPositions.add(p.add(0, 1, 0)); pathEndPosition = p; @@ -57,18 +55,22 @@ protected void noPathFound() { @Override protected float rateDestination(int distance, int x, int y, int z) { if (isInArea(x, y, z) - && (!isTemporaryCleared(x, y, z) || (!isTemporaryCleared(x, - y + 1, z) && y < maxPos.y))) { - float bonus = 0.0001f * (x - minPos.x) + 0.001f * (y - minPos.y); + && (!isTemporaryCleared(x, y, z) || !isTemporaryCleared(x, + y + 1, z) && y < maxPos.y)) { + final float bonus = 0.0001f * (x - minPos.x) + 0.001f + * (y - minPos.y); int layerMalus; - if (topY <= y) + if (topY <= y) { layerMalus = 5; - else if (!isInArea(x, y+1, z) || isTemporaryCleared(x, y + 1, z)) + } else if (!isInArea(x, y + 1, z) + || isTemporaryCleared(x, y + 1, z)) { layerMalus = 2; - else if (isInArea(x, y+1, z) && !isTemporaryCleared(x, y + 2, z)) + } else if (isInArea(x, y + 1, z) + && !isTemporaryCleared(x, y + 2, z)) { layerMalus = 2; - else + } else { layerMalus = 0; + } return distance + bonus + layerMalus + (maxPos.y - y) * 2; } else { return -1; @@ -76,7 +78,7 @@ else if (isInArea(x, y+1, z) && !isTemporaryCleared(x, y + 2, z)) } private boolean isTemporaryCleared(int x, int y, int z) { - return isClearedBlock(x, y, z) + return isClearedBlock(helper, x, y, z) || foundPositions.contains(new Pos(x, y, z)); } @@ -85,7 +87,7 @@ private boolean isInArea(int x, int y, int z) { && minPos.z <= z && z <= maxPos.z; } - private boolean isClearedBlock(int x, int y, int z) { + private static boolean isClearedBlock(AIHelper helper, int x, int y, int z) { return AIHelper.blockIsOneOf(helper.getBlock(x, y, z), Blocks.air, Blocks.torch); } @@ -95,12 +97,12 @@ protected void addTasksForTarget(Pos currentPos) { super.addTasksForTarget(currentPos); Pos top = currentPos; for (int i = 1; i < 6; i++) { - Pos pos = currentPos.add(0, i, 0); + final Pos pos = currentPos.add(0, i, 0); if (pos.y <= maxPos.y) { top = pos; } } - helper.addTask(new DestroyInRangeTask(currentPos, top)); + addTask(new DestroyInRangeTask(currentPos, top)); } @Override @@ -113,13 +115,13 @@ public int getAreaSize() { * (maxPos.z - minPos.z + 1); } - public float getToClearCount() { + public float getToClearCount(AIHelper helper) { int count = 0; int newTopY = minPos.y; for (int y = minPos.y; y <= maxPos.y; y++) { for (int z = minPos.z; z <= maxPos.z; z++) { for (int x = minPos.x; x <= maxPos.x; x++) { - if (!isClearedBlock(x, y, z)) { + if (!isClearedBlock(helper, x, y, z)) { count++; newTopY = Math.max(y, newTopY); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/HealthWatcher.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/HealthWatcher.java index 9a42c9b0..67480375 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/HealthWatcher.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/HealthWatcher.java @@ -2,8 +2,6 @@ import net.famzangl.minecraft.minebot.MinebotSettings; import net.famzangl.minecraft.minebot.ai.task.AITask; -import net.famzangl.minecraft.minebot.ai.task.LogOutTask; -import net.famzangl.minecraft.minebot.ai.task.SendCommandTask; public class HealthWatcher { private static final float EPSILON = 0.01f; @@ -15,21 +13,21 @@ public class HealthWatcher { public AITask getOverrideTask(float health) { health -= EPSILON; - final int commandTh = settings.getInt("on_damage_command_level", -1); - if (health <= commandTh && !commandSend) { - commandSend = true; - return new SendCommandTask(settings.get("on_damage_command", "")); - } else if (health > commandTh) { - commandSend = false; - } - final int logoutTh = settings.getInt("on_damage_logout_level", -1); - if (health <= logoutTh && !logoutSend) { - logoutSend = true; - System.out.println("Logging out. Current health: " + health); - return new LogOutTask(); - } else if (health > commandTh) { - logoutSend = false; - } + // final int commandTh = settings.getInt("on_damage_command_level", -1); + // if (health <= commandTh && !commandSend) { + // commandSend = true; + // return new SendCommandTask(settings.get("on_damage_command", "")); + // } else if (health > commandTh) { + // commandSend = false; + // } + // final int logoutTh = settings.getInt("on_damage_logout_level", -1); + // if (health <= logoutTh && !logoutSend) { + // logoutSend = true; + // System.out.println("Logging out. Current health: " + health); + // return new LogOutTask(); + // } else if (health > commandTh) { + // logoutSend = false; + // } return null; } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/LayRailPathFinder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/LayRailPathFinder.java index 1aba9761..69faa291 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/LayRailPathFinder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/LayRailPathFinder.java @@ -1,7 +1,6 @@ package net.famzangl.minecraft.minebot.ai.path; import net.famzangl.minecraft.minebot.Pos; -import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.BlockItemFilter; import net.famzangl.minecraft.minebot.ai.task.MineBlockTask; import net.famzangl.minecraft.minebot.ai.task.move.DownwardsMoveTask; @@ -12,10 +11,8 @@ public class LayRailPathFinder extends AlongTrackPathFinder { - - public LayRailPathFinder(AIHelper helper, int dx, int dz, int cx, int cy, - int cz) { - super(helper, dx, dz, cx, cy, cz); + public LayRailPathFinder(int dx, int dz, int cx, int cy, int cz) { + super(dx, dz, cx, cy, cz); } @Override @@ -54,7 +51,7 @@ private boolean placeAccRail(int x, int z) { protected void addTasksForTarget(Pos currentPos) { if (isRedstoneBlockPosition(currentPos.x, currentPos.y, currentPos.z)) { // For those server lags - helper.addTask(new UpwardsMoveTask(currentPos.x, currentPos.y + 1, + addTask(new UpwardsMoveTask(currentPos.x, currentPos.y + 1, currentPos.z, new BlockItemFilter(Blocks.redstone_block))); } else if (placeAccRail(currentPos.x, currentPos.z)) { if (!Block.isEqualTo(helper.getBlock(currentPos.x, @@ -63,9 +60,9 @@ protected void addTasksForTarget(Pos currentPos) { currentPos.z) && helper.isSafeGroundBlock(currentPos.x, currentPos.y - 2, currentPos.z)) { - helper.addTask(new DownwardsMoveTask(currentPos.x, - currentPos.y - 1, currentPos.z)); - helper.addTask(new UpwardsMoveTask(currentPos.x, currentPos.y, + addTask(new DownwardsMoveTask(currentPos.x, currentPos.y - 1, + currentPos.z)); + addTask(new UpwardsMoveTask(currentPos.x, currentPos.y, currentPos.z, new BlockItemFilter(Blocks.redstone_block))); } @@ -77,10 +74,9 @@ protected void addTasksForTarget(Pos currentPos) { private void placeRail(Pos currentPos, Block rail) { if (!helper.isAirBlock(currentPos.x, currentPos.y, currentPos.z)) { - helper.addTask(new MineBlockTask(currentPos.x, currentPos.y, - currentPos.z)); + addTask(new MineBlockTask(currentPos.x, currentPos.y, currentPos.z)); } - helper.addTask(new PlaceBlockAtFloorTask(currentPos.x, currentPos.y, + addTask(new PlaceBlockAtFloorTask(currentPos.x, currentPos.y, currentPos.z, new BlockItemFilter(rail))); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MineBySettingsPathFinder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MineBySettingsPathFinder.java index 311da893..319591c6 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MineBySettingsPathFinder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MineBySettingsPathFinder.java @@ -1,6 +1,5 @@ package net.famzangl.minecraft.minebot.ai.path; -import net.famzangl.minecraft.minebot.ai.AIHelper; import net.minecraft.block.Block; import net.minecraftforge.common.util.ForgeDirection; @@ -22,8 +21,8 @@ public FileSettingsProvider(String name_prefix, float defaultValue, @Override public float getFloat(Block block) { - String name = Block.blockRegistry.getNameForObject(block).replace( - "minecraft:", ""); + final String name = Block.blockRegistry.getNameForObject(block) + .replace("minecraft:", ""); final float val = settings.getFloat(name_prefix + name, defaultValue, min, max); System.out.println(name_prefix + name + " -> " + val); @@ -31,8 +30,9 @@ public float getFloat(Block block) { } } - public MineBySettingsPathFinder(AIHelper helper, ForgeDirection preferedDirection, int preferedLayer) { - super(helper, preferedDirection, preferedLayer); + public MineBySettingsPathFinder(ForgeDirection preferedDirection, + int preferedLayer) { + super(preferedDirection, preferedLayer); maxDistancePoints = 0; maxDistanceFactor = MIN_FACTOR; for (final String s : settings.getKeys()) { diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MinePathfinder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MinePathfinder.java index c74bee48..7a85b3be 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MinePathfinder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MinePathfinder.java @@ -3,7 +3,6 @@ import java.util.Random; import net.famzangl.minecraft.minebot.Pos; -import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.task.DestroyInRangeTask; import net.minecraft.block.Block; import net.minecraftforge.common.util.ForgeDirection; @@ -24,7 +23,7 @@ public abstract class MinePathfinder extends MovePathFinder { * 0.0 - 1.0. */ protected final float preferedDirectionInfluence = 0.3f; - + protected final int preferedLayer; /** * 0.0 - 1.0. @@ -52,18 +51,18 @@ public float getForBlock(int x, int y, int z) { final int id = helper.getBlockId(x, y, z); if (!isCached[id]) { -// final String name = Block.blockRegistry.getNameForObject( -// Block.blockRegistry.getObjectById(id)).replace( -// "minecraft:", ""); - cached[id] = settingsProvider.getFloat((Block) Block.blockRegistry.getObjectById(id)); + // final String name = Block.blockRegistry.getNameForObject( + // Block.blockRegistry.getObjectById(id)).replace( + // "minecraft:", ""); + cached[id] = settingsProvider + .getFloat((Block) Block.blockRegistry.getObjectById(id)); isCached[id] = true; } return cached[id]; } } - public MinePathfinder(AIHelper helper, ForgeDirection preferedDirection, int preferedLayer) { - super(helper); + public MinePathfinder(ForgeDirection preferedDirection, int preferedLayer) { this.preferedDirection = preferedDirection; this.preferedLayer = preferedLayer; points = new FloatBlockCache(getPointsProvider()); @@ -92,22 +91,24 @@ protected float rateDestination(int distance, int x, int y, int z) { return -1; } else { float badDirectionMalus = 0; - Pos current = helper.getPlayerPosition(); + final Pos current = helper.getPlayerPosition(); if (preferedDirection != null && preferedDirection.offsetX != 0) { - int dx = x - current.x; + final int dx = x - current.x; if (Math.signum(dx) != preferedDirection.offsetX) { badDirectionMalus = dx * preferedDirectionInfluence; } - } else if (preferedDirection != null && preferedDirection.offsetZ != 0) { - int dz = z - current.z; + } else if (preferedDirection != null + && preferedDirection.offsetZ != 0) { + final int dz = z - current.z; if (Math.signum(dz) != preferedDirection.offsetZ) { badDirectionMalus = dz * preferedDirectionInfluence; } } - float badY = Math.abs(y - preferedLayer) * preferedLayerInfluence; - - - return makeRandom(rating + addForDoubleMine + badDirectionMalus + badY); + final float badY = Math.abs(y - preferedLayer) + * preferedLayerInfluence; + + return makeRandom(rating + addForDoubleMine + badDirectionMalus + + badY); } } @@ -143,7 +144,7 @@ protected void addTasksForTarget(Pos currentPos) { } else { bottom = currentPos.add(0, 1, 0); } - + for (int i = 2; i < 5; i++) { if (!helper.hasSafeSides(currentPos.x, currentPos.y + i, currentPos.z) @@ -157,7 +158,7 @@ protected void addTasksForTarget(Pos currentPos) { break; } } - helper.addTask(new DestroyInRangeTask(bottom, top)); + addTask(new DestroyInRangeTask(bottom, top)); } @Override diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MineSinglePathFinder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MineSinglePathFinder.java index 25b038f5..72ca25b0 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MineSinglePathFinder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MineSinglePathFinder.java @@ -1,6 +1,5 @@ package net.famzangl.minecraft.minebot.ai.path; -import net.famzangl.minecraft.minebot.ai.AIHelper; import net.minecraft.block.Block; import net.minecraftforge.common.util.ForgeDirection; @@ -8,8 +7,9 @@ public class MineSinglePathFinder extends MinePathfinder { private final Block block; - public MineSinglePathFinder(AIHelper helper, Block block, ForgeDirection preferedDirection, int preferedLayer) { - super(helper, preferedDirection, preferedLayer); + public MineSinglePathFinder(Block block, ForgeDirection preferedDirection, + int preferedLayer) { + super(preferedDirection, preferedLayer); this.block = block; } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MovePathFinder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MovePathFinder.java index 88a2f364..e14fb9d2 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MovePathFinder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/MovePathFinder.java @@ -8,6 +8,7 @@ import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.BlockItemFilter; import net.famzangl.minecraft.minebot.ai.PathFinderField; +import net.famzangl.minecraft.minebot.ai.task.AITask; import net.famzangl.minecraft.minebot.ai.task.move.AlignToGridTask; import net.famzangl.minecraft.minebot.ai.task.move.DownwardsMoveTask; import net.famzangl.minecraft.minebot.ai.task.move.HorizontalMoveTask; @@ -40,16 +41,18 @@ public class MovePathFinder extends PathFinderField { Blocks.cactus, Blocks.obsidian, Blocks.piston_extension, Blocks.piston_head }; - public MovePathFinder(AIHelper helper) { + private TaskReceiver receiver; + + public MovePathFinder() { super(); - this.helper = helper; settings = new MinebotSettings(); - ArrayList blocks = new ArrayList(); - String upwardsBuildBlockNames = settings.get("upwards_place_block", - "dirt,stone,cobblestone"); - for (String name : upwardsBuildBlockNames.split("\\s*[\\,\\s\\;]\\s*")) { - Block block = (Block) Block.blockRegistry.getObject(name); + final ArrayList blocks = new ArrayList(); + final String upwardsBuildBlockNames = settings.get( + "upwards_place_block", "dirt,stone,cobblestone"); + for (final String name : upwardsBuildBlockNames + .split("\\s*[\\,\\s\\;]\\s*")) { + final Block block = (Block) Block.blockRegistry.getObject(name); if (block != null) { blocks.add(block); } else { @@ -60,6 +63,27 @@ public MovePathFinder(AIHelper helper) { torchLightLevel = settings.getFloat("place_torches_at", 1.0f, -1, 15); } + @Override + protected final boolean searchSomethingAround(int cx, int cy, int cz) { + throw new UnsupportedOperationException("Direct call not supported."); + } + + protected void addTask(AITask task) { + receiver.addTask(task); + } + + public final boolean searchSomethingAround(Pos playerPosition, + AIHelper helper, TaskReceiver receiver) { + this.helper = helper; + this.receiver = receiver; + return runSearch(playerPosition); + } + + protected boolean runSearch(Pos playerPosition) { + return super.searchSomethingAround(playerPosition.x, playerPosition.y, + playerPosition.z); + } + @Override protected int getNeighbour(int currentNode, int cx, int cy, int cz) { final int res = super.getNeighbour(currentNode, cx, cy, cz); @@ -107,11 +131,10 @@ protected void noPathFound() { protected void foundPath(LinkedList path) { super.foundPath(path); Pos currentPos = path.removeFirst(); - helper.addTask(new AlignToGridTask(currentPos.x, currentPos.y, - currentPos.z)); + addTask(new AlignToGridTask(currentPos.x, currentPos.y, currentPos.z)); while (!path.isEmpty()) { Pos nextPos = path.removeFirst(); - ForgeDirection moveDirection = direction(currentPos, nextPos); + final ForgeDirection moveDirection = direction(currentPos, nextPos); if (torchLightLevel >= 0) { ForgeDirection direction; @@ -120,28 +143,27 @@ protected void foundPath(LinkedList path) { } else { direction = moveDirection; } - helper.addTask(new PlaceTorchIfLightBelowTask(currentPos, - direction, torchLightLevel)); + addTask(new PlaceTorchIfLightBelowTask(currentPos, direction, + torchLightLevel)); } - Pos peeked = path.peekFirst(); + final Pos peeked = path.peekFirst(); if (moveDirection == ForgeDirection.UP && peeked != null && direction(nextPos, peeked).offsetY == 0) { // Combine upwards-sidewards. - System.out.println("Next direction is: " + direction(nextPos, peeked)); - helper.addTask(new JumpMoveTask(peeked.x, peeked.y, peeked.z, + System.out.println("Next direction is: " + + direction(nextPos, peeked)); + addTask(new JumpMoveTask(peeked.x, peeked.y, peeked.z, nextPos.x, nextPos.z)); nextPos = peeked; path.removeFirst(); } else if (nextPos.y > currentPos.y) { - helper.addTask(new UpwardsMoveTask(nextPos.x, nextPos.y, - nextPos.z, new BlockItemFilter(upwardsBuildBlocks))); + addTask(new UpwardsMoveTask(nextPos.x, nextPos.y, nextPos.z, + new BlockItemFilter(upwardsBuildBlocks))); } else if (nextPos.y < currentPos.y) { - helper.addTask(new DownwardsMoveTask(nextPos.x, nextPos.y, - nextPos.z)); + addTask(new DownwardsMoveTask(nextPos.x, nextPos.y, nextPos.z)); } else { - helper.addTask(new HorizontalMoveTask(nextPos.x, nextPos.y, - nextPos.z)); + addTask(new HorizontalMoveTask(nextPos.x, nextPos.y, nextPos.z)); } currentPos = nextPos; } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/PlaceTorchIfLightBelowTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/PlaceTorchIfLightBelowTask.java index 930a9d40..3e7b51ed 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/PlaceTorchIfLightBelowTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/PlaceTorchIfLightBelowTask.java @@ -28,8 +28,8 @@ public PlaceTorchIfLightBelowTask(Pos currentPos, } private static ForgeDirection[] getDirections(ForgeDirection except) { - ArrayList allowed = new ArrayList(); - for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) { + final ArrayList allowed = new ArrayList(); + for (final ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) { if (d != except && d != ForgeDirection.UP) { allowed.add(d); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/PlantPathFinder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/PlantPathFinder.java index 0db52b60..c14f3326 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/PlantPathFinder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/PlantPathFinder.java @@ -29,7 +29,7 @@ private PlantType(Item... items) { } public boolean canPlantItem(Item item) { - for (Item i : items) { + for (final Item i : items) { if (item == i) { return true; } @@ -61,8 +61,7 @@ public boolean matches(ItemStack itemStack) { private final PlantType type; - public PlantPathFinder(AIHelper helper, PlantType type) { - super(helper); + public PlantPathFinder(PlantType type) { this.type = type; } @@ -108,13 +107,13 @@ protected boolean isForbiddenBlock(Block block) { protected void addTasksForTarget(Pos currentPos) { if (helper.isAirBlock(currentPos.x, currentPos.y, currentPos.z)) { if (!hasFarmlandBelow(currentPos.x, currentPos.y, currentPos.z)) { - helper.addTask(new UseItemOnBlockAtTask(new HoeFilter(), - currentPos.x, currentPos.y - 1, currentPos.z)); + addTask(new UseItemOnBlockAtTask(new HoeFilter(), currentPos.x, + currentPos.y - 1, currentPos.z)); } - helper.addTask(new PlaceBlockAtFloorTask(currentPos.x, - currentPos.y, currentPos.z, new SeedFilter(type))); + addTask(new PlaceBlockAtFloorTask(currentPos.x, currentPos.y, + currentPos.z, new SeedFilter(type))); } else { - helper.addTask(new DestroyBlockTask(currentPos.x, currentPos.y, + addTask(new DestroyBlockTask(currentPos.x, currentPos.y, currentPos.z)); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/TaskReceiver.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/TaskReceiver.java new file mode 100644 index 00000000..9acc4ef7 --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/TaskReceiver.java @@ -0,0 +1,14 @@ +package net.famzangl.minecraft.minebot.ai.path; + +import net.famzangl.minecraft.minebot.ai.task.AITask; + +public interface TaskReceiver { + + /** + * Adds a task that should be executed. + * + * @param task + * The new task + */ + public abstract void addTask(AITask task); +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/TreePathFinder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/TreePathFinder.java index eff4869a..f1165499 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/TreePathFinder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/TreePathFinder.java @@ -1,7 +1,6 @@ package net.famzangl.minecraft.minebot.ai.path; import net.famzangl.minecraft.minebot.Pos; -import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.task.MineBlockTask; import net.famzangl.minecraft.minebot.build.WoodType; import net.minecraft.block.Block; @@ -10,8 +9,7 @@ public class TreePathFinder extends MovePathFinder { private final WoodType type; - public TreePathFinder(AIHelper helper, WoodType type) { - super(helper); + public TreePathFinder(WoodType type) { this.type = type; } @@ -68,8 +66,8 @@ protected void addTasksForTarget(Pos currentPos) { } if (!helper .isAirBlock(currentPos.x, currentPos.y + i, currentPos.z)) { - helper.addTask(new MineBlockTask(currentPos.x, - currentPos.y + i, currentPos.z)); + addTask(new MineBlockTask(currentPos.x, currentPos.y + i, + currentPos.z)); } } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/TunnelPathFinder.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/TunnelPathFinder.java index 8e319444..27b61cbb 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/path/TunnelPathFinder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/path/TunnelPathFinder.java @@ -34,9 +34,9 @@ private TorchSide(boolean left, boolean right, boolean floor) { } } - public TunnelPathFinder(AIHelper helper, int dx, int dz, int cx, int cy, - int cz, int addToSide, int addToTop, TorchSide torches) { - super(helper, dx, dz, cx, cy, cz); + public TunnelPathFinder(int dx, int dz, int cx, int cy, int cz, + int addToSide, int addToTop, TorchSide torches) { + super(dx, dz, cx, cy, cz); this.addToSide = addToSide; this.addToTop = addToTop; this.torches = torches; @@ -44,7 +44,10 @@ public TunnelPathFinder(AIHelper helper, int dx, int dz, int cx, int cy, @Override protected float rateDestination(int distance, int x, int y, int z) { - if (isOnTrack(x, z) && y == cy && !AIHelper.blockIsOneOf(helper.getBlock(x, y, z), Blocks.air, Blocks.torch)) { + if (isOnTrack(x, z) + && y == cy + && !AIHelper.blockIsOneOf(helper.getBlock(x, y, z), Blocks.air, + Blocks.torch)) { return distance + 1; } else { return -1; @@ -63,9 +66,9 @@ protected void addTasksForTarget(Pos currentPos) { p2 = new Pos(currentPos.x, currentPos.y + 1 + addToTop, currentPos.z - addToSide); } - helper.addTask(new DestroyInRangeTask(p1, p2)); + addTask(new DestroyInRangeTask(p1, p2)); - boolean isTorchStep = getStepNumber(currentPos.x, currentPos.z) % 8 == 0; + final boolean isTorchStep = getStepNumber(currentPos.x, currentPos.z) % 8 == 0; if (torches.right && isTorchStep) { addTorchesTask(currentPos, -dz, dx); } @@ -73,12 +76,13 @@ protected void addTasksForTarget(Pos currentPos) { addTorchesTask(currentPos, dz, -dx); } if (torches.floor && isTorchStep) { - helper.addTask(new PlaceTorchSomewhereTask(Collections.singletonList(currentPos), ForgeDirection.DOWN)); + addTask(new PlaceTorchSomewhereTask( + Collections.singletonList(currentPos), ForgeDirection.DOWN)); } } private void addTorchesTask(Pos currentPos, int dirX, int dirZ) { - ArrayList positions = new ArrayList(); + final ArrayList positions = new ArrayList(); positions.add(new Pos(currentPos.x + dirX * addToSide, currentPos.y + 1, currentPos.z + dirZ * addToSide)); @@ -86,7 +90,8 @@ private void addTorchesTask(Pos currentPos, int dirX, int dirZ) { positions.add(new Pos(currentPos.x + dirX * i, currentPos.y, currentPos.z + dirZ * i)); } - helper.addTask(new PlaceTorchSomewhereTask(positions, AIHelper.getDirectionForXZ(dirX, dirZ), ForgeDirection.DOWN)); + addTask(new PlaceTorchSomewhereTask(positions, + AIHelper.getDirectionForXZ(dirX, dirZ), ForgeDirection.DOWN)); } @Override diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/render/BuildMarkerRenderer.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/render/BuildMarkerRenderer.java index c50609ac..103d66fb 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/render/BuildMarkerRenderer.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/render/BuildMarkerRenderer.java @@ -25,12 +25,12 @@ public CornerComparator(int[] orders, boolean[] inverts) { @Override public int compare(BuildTask o1, BuildTask o2) { - Pos p1 = o1.getForPosition(); - Pos p2 = o2.getForPosition(); - int[] points = new int[] { p1.x, p1.y, p1.z }; - int[] points2 = new int[] { p2.x, p2.y, p2.z }; + final Pos p1 = o1.getForPosition(); + final Pos p2 = o2.getForPosition(); + final int[] points = new int[] { p1.x, p1.y, p1.z }; + final int[] points2 = new int[] { p2.x, p2.y, p2.z }; for (int i = 0; i < 3; i++) { - int res = ((Integer) points[orders[i]]) + final int res = ((Integer) points[orders[i]]) .compareTo(points2[orders[i]]); if (res != 0) { return inverts[i] ? -res : res; @@ -42,16 +42,16 @@ public int compare(BuildTask o1, BuildTask o2) { public void render(RenderWorldLastEvent event, AIHelper helper) { renderStart(event, helper); - BuildManager buildManager = helper.buildManager; - List scheduled = buildManager.getScheduled(); + final BuildManager buildManager = helper.buildManager; + final List scheduled = buildManager.getScheduled(); if (scheduled.size() > 0) { - BuildTask nextTask = scheduled.get(0); - HashSet corners = new HashSet(); + final BuildTask nextTask = scheduled.get(0); + final HashSet corners = new HashSet(); renderMarker(nextTask.getForPosition(), 1, 1, 0, 0.7f); findCorners(corners, scheduled); corners.remove(nextTask.getForPosition()); - for (Pos c : corners) { + for (final Pos c : corners) { renderMarker(c, 0, 0, 1, 0.5f); } } @@ -90,7 +90,7 @@ private void addWith(HashSet corners, List scheduled, private void addWith(HashSet corners, List scheduled, int side1, boolean side1Inverted, int side2, boolean side2Inverted, int side3, boolean side3Inverted) { - BuildTask p = Collections.min(scheduled, new CornerComparator( + final BuildTask p = Collections.min(scheduled, new CornerComparator( new int[] { side1, side2, side3 }, new boolean[] { side1Inverted, side2Inverted, side3Inverted })); corners.add(p.getForPosition()); diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/render/RenderHelper.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/render/RenderHelper.java index 06fa07ed..3cde9484 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/render/RenderHelper.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/render/RenderHelper.java @@ -24,7 +24,7 @@ public class RenderHelper { public void renderStart(RenderWorldLastEvent event, AIHelper helper) { final Tessellator tessellator = Tessellator.instance; - EntityLivingBase player = helper.getMinecraft().renderViewEntity; + final EntityLivingBase player = helper.getMinecraft().renderViewEntity; final double x = player.lastTickPosX + (player.posX - player.lastTickPosX) * event.partialTicks; final double y = player.lastTickPosY diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/AndSelector.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/AndSelector.java index c08404d6..d6fa34ad 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/AndSelector.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/AndSelector.java @@ -13,7 +13,7 @@ public AndSelector(IEntitySelector... selectors) { @Override public boolean isEntityApplicable(Entity var1) { - for (IEntitySelector s : selectors) { + for (final IEntitySelector s : selectors) { if (!s.isEntityApplicable(var1)) { return false; } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/FeedableSelector.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/FeedableSelector.java index 0c0f25dd..12010a28 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/FeedableSelector.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/FeedableSelector.java @@ -1,16 +1,15 @@ package net.famzangl.minecraft.minebot.ai.selectors; +import net.famzangl.minecraft.minebot.ai.AIHelper; import net.minecraft.command.IEntitySelector; import net.minecraft.entity.Entity; import net.minecraft.entity.passive.EntityAnimal; -import net.minecraft.entity.passive.EntityWolf; -import net.minecraft.item.ItemStack; public final class FeedableSelector implements IEntitySelector { - private final ItemStack currentItem; + private final AIHelper helper; - public FeedableSelector(ItemStack currentItem) { - this.currentItem = currentItem; + public FeedableSelector(AIHelper helper) { + this.helper = helper; } @Override @@ -18,12 +17,6 @@ public boolean isEntityApplicable(Entity e) { if (!(e instanceof EntityAnimal)) { return false; } - final EntityAnimal animal = (EntityAnimal) e; - return animal.isBreedingItem(currentItem) && !animal.isInLove() - && (animal.getGrowingAge() == 0 || isHungryWolf(animal)) && animal.getHealth() > 0; - } - - private boolean isHungryWolf(EntityAnimal animal) { - return animal instanceof EntityWolf && animal.getHealth() < animal.getMaxHealth(); + return helper.canSelectItem(new FilterFeedingItem((EntityAnimal) e)); } } \ No newline at end of file diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/FilterFeedingItem.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/FilterFeedingItem.java new file mode 100644 index 00000000..0aa99815 --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/FilterFeedingItem.java @@ -0,0 +1,32 @@ +package net.famzangl.minecraft.minebot.ai.selectors; + +import net.famzangl.minecraft.minebot.ai.ItemFilter; +import net.minecraft.entity.passive.EntityAnimal; +import net.minecraft.entity.passive.EntityWolf; +import net.minecraft.item.ItemStack; + +public final class FilterFeedingItem implements ItemFilter { + private final EntityAnimal animal; + + public FilterFeedingItem(EntityAnimal animal) { + this.animal = animal; + } + + @Override + public boolean matches(ItemStack itemStack) { + return isFeedingItem(animal, itemStack); + } + + private boolean isFeedingItem(final EntityAnimal animal, + ItemStack currentItem) { + return currentItem != null && currentItem.getItem() != null + && animal.isBreedingItem(currentItem) && !animal.isInLove() + && (animal.getGrowingAge() == 0 || isHungryWolf(animal)) + && animal.getHealth() > 0; + } + + private boolean isHungryWolf(EntityAnimal animal) { + return animal instanceof EntityWolf + && animal.getHealth() < animal.getMaxHealth(); + } +} \ No newline at end of file diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/ItemSelector.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/ItemSelector.java index 5a230aa1..2e160841 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/ItemSelector.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/ItemSelector.java @@ -4,8 +4,7 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.item.EntityItem; -public final class ItemSelector implements - IEntitySelector { +public final class ItemSelector implements IEntitySelector { @Override public boolean isEntityApplicable(Entity e) { return e instanceof EntityItem; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/NotSelector.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/NotSelector.java index 9430a296..9f2c5728 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/NotSelector.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/NotSelector.java @@ -4,12 +4,12 @@ import net.minecraft.entity.Entity; public class NotSelector implements IEntitySelector { - + private final IEntitySelector selector; public NotSelector(IEntitySelector selector) { this.selector = selector; - + } @Override @@ -17,5 +17,4 @@ public boolean isEntityApplicable(Entity var1) { return !selector.isEntityApplicable(var1); } - } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/OneOfListSelector.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/OneOfListSelector.java index 4650d209..d854ee53 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/OneOfListSelector.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/OneOfListSelector.java @@ -6,9 +6,9 @@ import net.minecraft.entity.Entity; public class OneOfListSelector implements IEntitySelector { - + private final List list; - + public OneOfListSelector(List list) { this.list = list; } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/OrSelector.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/OrSelector.java index c49f03ff..e92ba4b6 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/OrSelector.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/OrSelector.java @@ -13,7 +13,7 @@ public OrSelector(IEntitySelector... selectors) { @Override public boolean isEntityApplicable(Entity var1) { - for (IEntitySelector s : selectors) { + for (final IEntitySelector s : selectors) { if (s.isEntityApplicable(var1)) { return true; } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/OwnTameableSelector.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/OwnTameableSelector.java index c243cbb4..5897ca81 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/OwnTameableSelector.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/selectors/OwnTameableSelector.java @@ -32,9 +32,9 @@ private boolean isMine(EntityTameable var1) { return var1.getOwner() == owner; } - } catch (NoSuchMethodException e1) { + } catch (final NoSuchMethodException e1) { e1.printStackTrace(); - } catch (SecurityException e1) { + } catch (final SecurityException e1) { e1.printStackTrace(); } return true; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/AIStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/AIStrategy.java new file mode 100644 index 00000000..d3523ae0 --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/AIStrategy.java @@ -0,0 +1,109 @@ +package net.famzangl.minecraft.minebot.ai.strategy; + +import net.famzangl.minecraft.minebot.ai.AIHelper; + +/** + * An {@link AIStrategy} tells the bot what to do next. It should recover from + * any state that might happen while the bot executes. + * + * @author michael + * + */ +public abstract class AIStrategy { + public enum TickResult { + /** + * To do this tick again, either calling the same strategy again or + * deactivating it. + */ + TICK_AGAIN, + /** + * The tick is handled, the player was moved and did some work. + */ + TICK_HANDLED, + /** + * This stategy did not do anything and has nothing more to do + * currently. Deactivate it. + */ + NO_MORE_WORK + }; + + private boolean active; + + /** + * (de) Activates the strategy. Always called in a game tick. This can be + * called any time, especially deactivate. + * + * @param active + */ + public final void setActive(boolean active, AIHelper helper) { + this.active = active; + if (active) { + onActivate(helper); + } else { + onDeactivate(helper); + } + } + + public final boolean isActive() { + return active; + } + + protected void onDeactivate(AIHelper helper) { + } + + protected void onActivate(AIHelper helper) { + } + + /** + * Tests if this strategy needs to take over immediately. Chances are good + * that you are standing on a cliff / ... if you get activated. + * + * @return true if {@link #checkShouldTakeOver(AIHelper)} needs + * to be called every game tick, false to only call on + * good/safe times. + */ + public boolean takesOverAnyTime() { + return false; + } + + /** + * Checks if this should take over. + * + * @param helper + * TODO + * + * @return + */ + public boolean checkShouldTakeOver(AIHelper helper) { + if (active) { + throw new IllegalStateException(); + } + return true; + } + + /** + * Runs a game tick. + * + * @param helper + * The helper. + * @return + */ + public final TickResult gameTick(AIHelper helper) { + if (!active) { + throw new IllegalStateException(); + } + + return onGameTick(helper); + } + + protected abstract TickResult onGameTick(AIHelper helper); + + /** + * + * @return A String to display in the top right hand corner of the screen. + */ + public String getDescription() { + return "No description so far... " + getClass().getSimpleName(); + } + +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/BuildWayStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/BuildWayStrategy.java index 2a5a1dc7..fd66ffb1 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/BuildWayStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/BuildWayStrategy.java @@ -1,22 +1,15 @@ package net.famzangl.minecraft.minebot.ai.strategy; -import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.path.BuildWayPathfinder; -import net.famzangl.minecraft.minebot.ai.task.AITask; -import net.famzangl.minecraft.minebot.ai.task.WaitTask; -import net.minecraftforge.common.util.ForgeDirection; -public class BuildWayStrategy implements AIStrategy { +public class BuildWayStrategy extends PathFinderStrategy { - private BuildWayPathfinder pathfinder; - private final ForgeDirection dir; - private final Pos pos; + private final BuildWayPathfinder pathfinder; - public BuildWayStrategy(ForgeDirection dir, Pos pos) { - this.dir = dir; - this.pos = pos; + public BuildWayStrategy(BuildWayPathfinder pathfinder) { + super(pathfinder, "Build a way."); + this.pathfinder = pathfinder; } @Override @@ -26,20 +19,9 @@ public String getDescription() { @Override public void searchTasks(AIHelper helper) { - if (pathfinder == null) { - boolean onSlab = true; - pathfinder = new BuildWayPathfinder(helper, dir.offsetX, - dir.offsetZ, pos.x, pos.y + 1, pos.z); + if (!pathfinder.addContinuingTask(helper.getPlayerPosition())) { + super.searchTasks(helper); } - if (!pathfinder.addContinuingTask(helper.getPlayerPosition()) && !pathfinder - .searchSomethingAround(helper.getPlayerPosition())) { - helper.addTask(WaitTask.instance); - } - } - - @Override - public AITask getOverrideTask(AIHelper helper) { - return null; } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/DamageTakenStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/DamageTakenStrategy.java new file mode 100644 index 00000000..0678e910 --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/DamageTakenStrategy.java @@ -0,0 +1,17 @@ +package net.famzangl.minecraft.minebot.ai.strategy; + +import net.famzangl.minecraft.minebot.ai.AIHelper; + +public class DamageTakenStrategy extends ValueActionStrategy { + + @Override + protected double getValue(AIHelper helper) { + return helper.getMinecraft().thePlayer.getHealth(); + } + + @Override + protected String getSettingPrefix() { + return "on_damage_"; + } + +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/FeedAnimalsStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/FeedAnimalsStrategy.java index 61f76b5b..3d4bcdf1 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/FeedAnimalsStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/FeedAnimalsStrategy.java @@ -1,48 +1,38 @@ package net.famzangl.minecraft.minebot.ai.strategy; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.selectors.AndSelector; import net.famzangl.minecraft.minebot.ai.selectors.ColorSelector; import net.famzangl.minecraft.minebot.ai.selectors.FeedableSelector; +import net.famzangl.minecraft.minebot.ai.selectors.FilterFeedingItem; import net.famzangl.minecraft.minebot.ai.selectors.OrSelector; import net.famzangl.minecraft.minebot.ai.selectors.XPOrbSelector; -import net.famzangl.minecraft.minebot.ai.task.AITask; import net.famzangl.minecraft.minebot.ai.task.FaceAndInteractTask; import net.minecraft.command.IEntitySelector; import net.minecraft.entity.Entity; -import net.minecraft.init.Items; -import net.minecraft.item.ItemFood; -import net.minecraft.item.ItemSeeds; -import net.minecraft.item.ItemStack; +import net.minecraft.entity.passive.EntityAnimal; -public class FeedAnimalsStrategy implements AIStrategy { +public class FeedAnimalsStrategy extends TaskStrategy { private static final int DISTANCE = 20; private final int color; public FeedAnimalsStrategy() { - this (-1); + this(-1); } - + public FeedAnimalsStrategy(int color) { this.color = color; } @Override public void searchTasks(AIHelper helper) { - final ItemStack currentItem = helper.getMinecraft().thePlayer.inventory - .getCurrentItem(); - if (currentItem != null - && (currentItem.getItem() instanceof ItemFood - || currentItem.getItem() == Items.wheat || currentItem - .getItem() instanceof ItemSeeds)) { - feedWithFood(helper, currentItem); - } + feedWithFood(helper); } + - private void feedWithFood(AIHelper helper, final ItemStack currentItem) { - IEntitySelector selector = new FeedableSelector(currentItem); + private void feedWithFood(AIHelper helper) { + IEntitySelector selector = new FeedableSelector(helper); if (color >= 0) { selector = new AndSelector(selector, new ColorSelector(color)); } @@ -53,7 +43,19 @@ private void feedWithFood(AIHelper helper, final ItemStack currentItem) { selector, collect)); if (found != null) { - helper.addTask(new FaceAndInteractTask(found, selector)); + addTask(new FaceAndInteractTask(found, selector) { + @Override + protected void doInteractWithCurrent(AIHelper h) { + final Entity over = h.getObjectMouseOver().entityHit; + if (over instanceof EntityAnimal + && h.selectCurrentItem(new FilterFeedingItem( + (EntityAnimal) over))) { + super.doInteractWithCurrent(h); + } else if (found == over) { + interacted = true; + } + } + }); } } @@ -61,10 +63,4 @@ private void feedWithFood(AIHelper helper, final ItemStack currentItem) { public String getDescription() { return "Feeding"; } - - @Override - public AITask getOverrideTask(AIHelper helper) { - return null; - } - } \ No newline at end of file diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/FishStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/FishStrategy.java index 5bb06c7c..075183a6 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/FishStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/FishStrategy.java @@ -1,25 +1,19 @@ package net.famzangl.minecraft.minebot.ai.strategy; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; -import net.famzangl.minecraft.minebot.ai.AIStrategyFactory; import net.famzangl.minecraft.minebot.ai.animals.ThrowFishingRodTask; -import net.famzangl.minecraft.minebot.ai.task.AITask; import net.famzangl.minecraft.minebot.ai.task.DoFishTask; import net.famzangl.minecraft.minebot.ai.task.WaitTask; -public class FishStrategy implements AIStrategy, AIStrategyFactory { +public class FishStrategy extends TaskStrategy { @Override public void searchTasks(AIHelper helper) { if (helper.getMinecraft().thePlayer.fishEntity == null) { - helper.addTask(new ThrowFishingRodTask()); - helper.addTask(WaitTask.instance); - helper.addTask(WaitTask.instance); - helper.addTask(WaitTask.instance); - helper.addTask(WaitTask.instance); + addTask(new ThrowFishingRodTask()); + addTask(new WaitTask(10)); } else { - helper.addTask(new DoFishTask()); + addTask(new DoFishTask()); } } @@ -28,14 +22,4 @@ public String getDescription() { return "Fishing"; } - @Override - public AITask getOverrideTask(AIHelper helper) { - return null; - } - - @Override - public AIStrategy produceStrategy(AIHelper helper) { - return this; - } - } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/KillAnimalsStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/KillAnimalsStrategy.java index 0fdb8d7d..61d4170d 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/KillAnimalsStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/KillAnimalsStrategy.java @@ -1,14 +1,14 @@ package net.famzangl.minecraft.minebot.ai.strategy; +import java.util.HashSet; + import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.animals.AnimalyType; import net.famzangl.minecraft.minebot.ai.selectors.AndSelector; import net.famzangl.minecraft.minebot.ai.selectors.ItemSelector; import net.famzangl.minecraft.minebot.ai.selectors.NotSelector; import net.famzangl.minecraft.minebot.ai.selectors.OrSelector; import net.famzangl.minecraft.minebot.ai.selectors.OwnTameableSelector; -import net.famzangl.minecraft.minebot.ai.task.AITask; import net.famzangl.minecraft.minebot.ai.task.FaceAndInteractTask; import net.minecraft.command.IEntitySelector; import net.minecraft.entity.Entity; @@ -20,7 +20,7 @@ * @author michael * */ -public class KillAnimalsStrategy implements AIStrategy { +public class KillAnimalsStrategy extends TaskStrategy { private final class KillableSelector implements IEntitySelector { @Override @@ -37,6 +37,7 @@ public boolean isEntityApplicable(Entity e) { private static final int DISTANCE = 20; private final int maxKills; private final AnimalyType type; + private final HashSet hitEntities = new HashSet(); public KillAnimalsStrategy() { this(-1, AnimalyType.ANY); @@ -49,6 +50,12 @@ public KillAnimalsStrategy(int maxKills, AnimalyType type) { @Override public void searchTasks(final AIHelper helper) { + if (maxKills >= 0) { + int kills = countKills(); + if (kills >= maxKills) { + return; + } + } final IEntitySelector selector = new AndSelector( new KillableSelector(), @@ -61,18 +68,30 @@ public void searchTasks(final AIHelper helper) { collect, selector)); if (found != null) { - helper.addTask(new FaceAndInteractTask(found, selector, false)); + addTask(new FaceAndInteractTask(found, selector, false) { + @Override + protected void doInteractWithCurrent(AIHelper h) { + hitEntities.add(h.getObjectMouseOver().entityHit); + super.doInteractWithCurrent(h); + } + }); } } - @Override - public String getDescription() { - return "Killing"; + private int countKills() { + int kills = 0; + for (Entity e : hitEntities) { + if (e instanceof EntityAnimal + && ((EntityAnimal) e).getHealth() <= 0) { + kills++; + } + } + return kills; } @Override - public AITask getOverrideTask(AIHelper helper) { - return null; + public String getDescription() { + return "Killing"; } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/LayRailStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/LayRailStrategy.java index 7dd016b6..953a62b5 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/LayRailStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/LayRailStrategy.java @@ -2,7 +2,6 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.AIStrategyFactory; import net.famzangl.minecraft.minebot.ai.path.LayRailPathFinder; import net.minecraftforge.common.util.ForgeDirection; @@ -12,9 +11,11 @@ public class LayRailStrategy implements AIStrategyFactory { @Override public AIStrategy produceStrategy(AIHelper helper) { final Pos p = helper.getPlayerPosition(); - ForgeDirection horizontalLook = helper.getLookDirection(); + final ForgeDirection horizontalLook = helper.getLookDirection(); - return new PathFinderStrategy(new LayRailPathFinder(helper, horizontalLook.offsetX, horizontalLook.offsetZ, - p.x, p.y, p.z), "Building a railway"); + return ValueActionStrategy.makeSafe(new PathFinderStrategy( + new LayRailPathFinder(horizontalLook.offsetX, + horizontalLook.offsetZ, p.x, p.y, p.z), + "Building a railway")); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/LetAnimalsSitStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/LetAnimalsSitStrategy.java index 6d558e62..c7b57888 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/LetAnimalsSitStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/LetAnimalsSitStrategy.java @@ -4,7 +4,6 @@ import java.util.List; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.ItemFilter; import net.famzangl.minecraft.minebot.ai.animals.AnimalyType; import net.famzangl.minecraft.minebot.ai.selectors.AndSelector; @@ -12,7 +11,6 @@ import net.famzangl.minecraft.minebot.ai.selectors.IsSittingSelector; import net.famzangl.minecraft.minebot.ai.selectors.NotSelector; import net.famzangl.minecraft.minebot.ai.selectors.OneOfListSelector; -import net.famzangl.minecraft.minebot.ai.task.AITask; import net.famzangl.minecraft.minebot.ai.task.FaceAndInteractTask; import net.minecraft.command.IEntitySelector; import net.minecraft.entity.Entity; @@ -27,7 +25,7 @@ * @author michael * */ -public class LetAnimalsSitStrategy implements AIStrategy { +public class LetAnimalsSitStrategy extends TaskStrategy { private final class NoWolfFoodFilter implements ItemFilter { @Override @@ -36,7 +34,7 @@ public boolean matches(ItemStack itemStack) { } private boolean interactsWithWolf(Item item) { - return (item instanceof ItemFood) + return item instanceof ItemFood && ((ItemFood) item).isWolfsFavoriteMeat() || item instanceof ItemDye; } @@ -82,7 +80,7 @@ public void searchTasks(AIHelper helper) { final Entity found = helper.getClosestEntity(DISTANCE, selector); if (found != null) { - helper.addTask(new FaceAndInteractTask(found, selector) { + addTask(new FaceAndInteractTask(found, selector) { @Override protected void doInteractWithCurrent(AIHelper h) { super.doInteractWithCurrent(h); @@ -96,10 +94,4 @@ protected void doInteractWithCurrent(AIHelper h) { public String getDescription() { return shouldSit ? "Let them sit" : "Let them go"; } - - @Override - public AITask getOverrideTask(AIHelper helper) { - return null; - } - } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/LumberjackStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/LumberjackStrategy.java index 72cf0752..628b2d06 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/LumberjackStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/LumberjackStrategy.java @@ -1,7 +1,6 @@ package net.famzangl.minecraft.minebot.ai.strategy; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.AIStrategyFactory; import net.famzangl.minecraft.minebot.ai.path.TreePathFinder; @@ -9,7 +8,7 @@ public class LumberjackStrategy implements AIStrategyFactory { @Override public AIStrategy produceStrategy(AIHelper helper) { - return new PathFinderStrategy(new TreePathFinder(helper, null), - "Getting some wood"); + return ValueActionStrategy.makeSafe(new PathFinderStrategy( + new TreePathFinder(null), "Getting some wood")); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/MineStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/MineStrategy.java index 1de852a8..ba37ab35 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/MineStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/MineStrategy.java @@ -1,7 +1,6 @@ package net.famzangl.minecraft.minebot.ai.strategy; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.AIStrategyFactory; import net.famzangl.minecraft.minebot.ai.path.MineBySettingsPathFinder; import net.famzangl.minecraft.minebot.ai.path.MineSinglePathFinder; @@ -11,14 +10,16 @@ public class MineStrategy implements AIStrategyFactory { @Override public AIStrategy produceStrategy(AIHelper helper) { - return new PathFinderStrategy(new MineBySettingsPathFinder(helper, - helper.getLookDirection(), helper.getPlayerPosition().y), "Mining ores"); + return ValueActionStrategy.makeSafe(new PathFinderStrategy( + new MineBySettingsPathFinder(helper.getLookDirection(), helper + .getPlayerPosition().y), "Mining ores")); } public AIStrategy produceStrategy(AIHelper helper, Block blockName) { - return new PathFinderStrategy(new MineSinglePathFinder(helper, - blockName, helper.getLookDirection(), helper.getPlayerPosition().y), "Mining " - + blockName.getLocalizedName()); + return ValueActionStrategy.makeSafe(new PathFinderStrategy( + new MineSinglePathFinder(blockName, helper.getLookDirection(), + helper.getPlayerPosition().y), "Mining " + + blockName.getLocalizedName())); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PathFinderStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PathFinderStrategy.java index c8e98a27..bfe562ee 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PathFinderStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PathFinderStrategy.java @@ -1,19 +1,14 @@ package net.famzangl.minecraft.minebot.ai.strategy; -import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; -import net.famzangl.minecraft.minebot.ai.path.HealthWatcher; import net.famzangl.minecraft.minebot.ai.path.MovePathFinder; -import net.famzangl.minecraft.minebot.ai.task.AITask; import net.famzangl.minecraft.minebot.ai.task.WaitTask; -import net.famzangl.minecraft.minebot.ai.task.place.DestroyBlockTask; -import net.minecraft.block.Block; -public class PathFinderStrategy implements AIStrategy { +public class PathFinderStrategy extends TaskStrategy { private final MovePathFinder pathFinder; private final String description; - private final HealthWatcher watcher = new HealthWatcher(); + + // private final HealthWatcher watcher = new HealthWatcher(); public PathFinderStrategy(MovePathFinder pathFinder, String description) { this.pathFinder = pathFinder; @@ -22,8 +17,9 @@ public PathFinderStrategy(MovePathFinder pathFinder, String description) { @Override public void searchTasks(AIHelper helper) { - if (!pathFinder.searchSomethingAround(helper.getPlayerPosition())) { - helper.addTask(WaitTask.instance); + if (!pathFinder.searchSomethingAround(helper.getPlayerPosition(), + helper, this)) { + addTask(new WaitTask(1)); } } @@ -32,20 +28,20 @@ public String getDescription() { return description; } - @Override - public AITask getOverrideTask(AIHelper helper) { - final Pos pos = helper.getPlayerPosition(); - final Block headBlock = helper.getBlock(pos.x, pos.y + 1, pos.z); - if (!helper.canWalkThrough(headBlock)) { - return new DestroyBlockTask(pos.x, pos.y + 1, pos.z); - } - final Block floorBlock = helper.getBlock(pos.x, pos.y, pos.z); - if (!helper.canWalkOn(floorBlock)) { - return new DestroyBlockTask(pos.x, pos.y, pos.z); - } - return watcher.getOverrideTask(helper.getMinecraft().thePlayer - .getHealth()); - } + // @Override + // public AITask getOverrideTask(AIHelper helper) { + // final Pos pos = helper.getPlayerPosition(); + // final Block headBlock = helper.getBlock(pos.x, pos.y + 1, pos.z); + // if (!helper.canWalkThrough(headBlock)) { + // return new DestroyBlockTask(pos.x, pos.y + 1, pos.z); + // } + // final Block floorBlock = helper.getBlock(pos.x, pos.y, pos.z); + // if (!helper.canWalkOn(floorBlock)) { + // return new DestroyBlockTask(pos.x, pos.y, pos.z); + // } + // return watcher.getOverrideTask(helper.getMinecraft().thePlayer + // .getHealth()); + // } @Override public String toString() { diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PauseStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PauseStrategy.java new file mode 100644 index 00000000..95ffb523 --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PauseStrategy.java @@ -0,0 +1,28 @@ +package net.famzangl.minecraft.minebot.ai.strategy; + +import net.famzangl.minecraft.minebot.ai.AIHelper; + +public class PauseStrategy extends AIStrategy { + + private int leftTicks; + + public PauseStrategy(int seconds) { + super(); + this.leftTicks = seconds * 20; + } + + @Override + public boolean checkShouldTakeOver(AIHelper helper) { + return leftTicks > 0; + } + + @Override + protected TickResult onGameTick(AIHelper helper) { + if (leftTicks > 0) { + leftTicks--; + return TickResult.TICK_HANDLED; + } + return TickResult.NO_MORE_WORK; + } + +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PlantStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PlantStrategy.java index 0d5b1744..07c16ccf 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PlantStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PlantStrategy.java @@ -1,7 +1,6 @@ package net.famzangl.minecraft.minebot.ai.strategy; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.AIStrategyFactory; import net.famzangl.minecraft.minebot.ai.path.PlantPathFinder; import net.famzangl.minecraft.minebot.ai.path.PlantPathFinder.PlantType; @@ -10,7 +9,8 @@ public class PlantStrategy implements AIStrategyFactory { @Override public AIStrategy produceStrategy(AIHelper helper) { - return new PathFinderStrategy(new PlantPathFinder(helper, PlantType.ANY), "Planting"); + return ValueActionStrategy.makeSafe(new PathFinderStrategy( + new PlantPathFinder(PlantType.ANY), "Planting")); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PlayerComesActionStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PlayerComesActionStrategy.java new file mode 100644 index 00000000..40ad05dc --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/PlayerComesActionStrategy.java @@ -0,0 +1,19 @@ +package net.famzangl.minecraft.minebot.ai.strategy; + +import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; + +public class PlayerComesActionStrategy extends CloseEntityActionStrategy { + + @Override + protected boolean matches(AIHelper helper, Entity player) { + return player instanceof EntityPlayer && player != helper.getMinecraft().thePlayer; + } + + @Override + protected String getSettingPrefix() { + return "on_player_comes_"; + } + +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/RunOnceStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/RunOnceStrategy.java new file mode 100644 index 00000000..021349de --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/RunOnceStrategy.java @@ -0,0 +1,24 @@ +package net.famzangl.minecraft.minebot.ai.strategy; + +import net.famzangl.minecraft.minebot.ai.AIHelper; + +public abstract class RunOnceStrategy extends AIStrategy { + + private boolean wasRun = false; + + @Override + protected TickResult onGameTick(AIHelper helper) { + if (!wasRun) { + wasRun = true; + this.singleRun(helper); + } + return TickResult.NO_MORE_WORK; + } + + protected abstract void singleRun(AIHelper helper); + + @Override + public boolean checkShouldTakeOver(AIHelper helper) { + return !wasRun; + } +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/ShearStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/ShearStrategy.java index e1b45c86..b776473a 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/ShearStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/ShearStrategy.java @@ -1,13 +1,11 @@ package net.famzangl.minecraft.minebot.ai.strategy; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.ItemFilter; import net.famzangl.minecraft.minebot.ai.selectors.AndSelector; import net.famzangl.minecraft.minebot.ai.selectors.ColorSelector; import net.famzangl.minecraft.minebot.ai.selectors.ItemSelector; import net.famzangl.minecraft.minebot.ai.selectors.OrSelector; -import net.famzangl.minecraft.minebot.ai.task.AITask; import net.famzangl.minecraft.minebot.ai.task.FaceAndInteractTask; import net.minecraft.command.IEntitySelector; import net.minecraft.entity.Entity; @@ -15,7 +13,7 @@ import net.minecraft.item.ItemShears; import net.minecraft.item.ItemStack; -public class ShearStrategy implements AIStrategy { +public class ShearStrategy extends TaskStrategy { private static final int DISTANCE = 20; private final int color; @@ -26,7 +24,8 @@ public ShearStrategy(int color) { private final class ShearsFilter implements ItemFilter { @Override public boolean matches(ItemStack itemStack) { - return itemStack != null && itemStack.getItem() instanceof ItemShears; + return itemStack != null + && itemStack.getItem() instanceof ItemShears; } } @@ -38,12 +37,6 @@ public boolean isEntityApplicable(Entity var1) { } } - public static enum TintType { - WOLF, - SHEEP, - ANY; - } - @Override public void searchTasks(AIHelper helper) { if (!helper.selectCurrentItem(new ShearsFilter())) { @@ -60,18 +53,13 @@ public void searchTasks(AIHelper helper) { selector, new ItemSelector())); if (found != null) { - helper.addTask(new FaceAndInteractTask(found, selector)); + addTask(new FaceAndInteractTask(found, selector)); } } @Override public String getDescription() { - return "Tinting..."; - } - - @Override - public AITask getOverrideTask(AIHelper helper) { - return null; + return "Shearing..."; } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/StackStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/StackStrategy.java new file mode 100644 index 00000000..74f5a4e6 --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/StackStrategy.java @@ -0,0 +1,46 @@ +package net.famzangl.minecraft.minebot.ai.strategy; + +import net.famzangl.minecraft.minebot.ai.AIHelper; + +/** + * Run a whole stack of strategies. + * + * @author michael + * + */ +public class StackStrategy extends AIStrategy { + + private final StrategyStack stack; + + public StackStrategy(StrategyStack stack) { + super(); + this.stack = stack; + } + + @Override + protected TickResult onGameTick(AIHelper helper) { + if (stack.gameTick(helper)) { + return TickResult.TICK_HANDLED; + } else { + return TickResult.NO_MORE_WORK; + } + } + + @Override + protected void onActivate(AIHelper helper) { + stack.resume(helper); + super.onActivate(helper); + } + + @Override + protected void onDeactivate(AIHelper helper) { + stack.pause(helper); + super.onDeactivate(helper); + } + + @Override + public String getDescription() { + AIStrategy current = stack.getCurrentStrategy(); + return current != null ? current.getDescription() : "Multiple tasks."; + } +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/StopStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/StopStrategy.java index 57a8f163..31339605 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/StopStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/StopStrategy.java @@ -1,21 +1,16 @@ package net.famzangl.minecraft.minebot.ai.strategy; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; -import net.famzangl.minecraft.minebot.ai.task.AITask; -public final class StopStrategy implements AIStrategy { - @Override - public void searchTasks(AIHelper helper) { - } +public final class StopStrategy extends AIStrategy { @Override - public AITask getOverrideTask(AIHelper helper) { - return null; + public String getDescription() { + return "Stopping"; } @Override - public String getDescription() { - return "Stopping"; + protected TickResult onGameTick(AIHelper helper) { + return TickResult.NO_MORE_WORK; } } \ No newline at end of file diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/StrategyStack.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/StrategyStack.java new file mode 100644 index 00000000..4d88bc0d --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/StrategyStack.java @@ -0,0 +1,88 @@ +package net.famzangl.minecraft.minebot.ai.strategy; + +import java.util.ArrayList; + +import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy.TickResult; + +public class StrategyStack { + /** + * Strategies, ordered from most to least important. + */ + private final ArrayList strategies = new ArrayList(); + private AIStrategy currentStrategy = null; + private boolean paused; + + /** + * Does a game tick. Selects the next best strategy to activate. + * + * @param helper + * @return true on success. + */ + public boolean gameTick(AIHelper helper) { + int i = 0; + do { + final TickResult result = strategyTick(helper, currentStrategy == null || i != 0); + i++; + if (result == null) { + return false; + } else if (result == TickResult.TICK_HANDLED) { + return true; + } else if (result == TickResult.NO_MORE_WORK) { + setCurrentStrategy(helper, null); + } else if (result == TickResult.TICK_AGAIN) { + } + } while (i < 100); + System.err.println("Help. Infinite loop in strategies."); + return false; + } + + private TickResult strategyTick(AIHelper helper, boolean goodTimeToPause) { + for (final AIStrategy s : strategies) { + if (s == currentStrategy) { + return s.gameTick(helper); + } else if ((goodTimeToPause || s.takesOverAnyTime()) + && s.checkShouldTakeOver(helper)) { + setCurrentStrategy(helper, s); + return s.gameTick(helper); + } + } + return null; + } + + private void setCurrentStrategy(AIHelper helper, AIStrategy s) { + if (currentStrategy != null) { + currentStrategy.setActive(false, helper); + } + currentStrategy = s; + if (currentStrategy != null) { + currentStrategy.setActive(true, helper); + } + } + + public void addStrategy(AIStrategy strategy) { + if (strategy.isActive()) { + throw new IllegalArgumentException( + "Attempting to add an active strategy to this stack."); + } else if (strategies.contains(strategy)) { + throw new IllegalArgumentException("Added a strategy twice."); + } + strategies.add(strategy); + } + + public void pause(AIHelper helper) { + setCurrentStrategy(helper, null); + this.paused = true; + } + + public void resume(AIHelper helper) { + this.paused = false; + } + + public void shutdown() { + } + + public AIStrategy getCurrentStrategy() { + return currentStrategy; + } +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/TaskOperations.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/TaskOperations.java new file mode 100644 index 00000000..f4c1c3ef --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/TaskOperations.java @@ -0,0 +1,22 @@ +package net.famzangl.minecraft.minebot.ai.strategy; + +import net.famzangl.minecraft.minebot.ai.task.error.TaskError; + +public interface TaskOperations { + + /** + * This should be called whenever the current task could not achieve it's + * goal. All following tasks are unscheduled. + * + * @param taskError + */ + public abstract void desync(TaskError taskError); + + /** + * This can be called by the current task to do a look ahead and already let + * the next task to it's face and destroy. Use with care. + * + * @return + */ + public boolean faceAndDestroyForNextTask(); +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/TaskStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/TaskStrategy.java new file mode 100644 index 00000000..22ac288d --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/TaskStrategy.java @@ -0,0 +1,142 @@ +package net.famzangl.minecraft.minebot.ai.strategy; + +import java.util.LinkedList; +import java.util.List; + +import net.famzangl.minecraft.minebot.Pos; +import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.command.AIChatController; +import net.famzangl.minecraft.minebot.ai.path.TaskReceiver; +import net.famzangl.minecraft.minebot.ai.task.AITask; +import net.famzangl.minecraft.minebot.ai.task.CanPrefaceAndDestroy; +import net.famzangl.minecraft.minebot.ai.task.SkipWhenSearchingPrefetch; +import net.famzangl.minecraft.minebot.ai.task.error.StringTaskError; +import net.famzangl.minecraft.minebot.ai.task.error.TaskError; + +/** + * This is a strategy that always queries for new tasks to do and then executes + * them. + * + * @author michael + * + */ +public abstract class TaskStrategy extends AIStrategy implements + TaskOperations, TaskReceiver { + private static final int MAX_LOOKAHEAD = 5; + private final LinkedList tasks = new LinkedList(); + + private boolean desync; + private boolean searchNewTasks = true; + private AIHelper temporaryHelper; + + private final LinkedList lastErrors = new LinkedList(); + private int taskTimeout; + + @Override + public void addTask(AITask task) { + if (task == null) { + throw new NullPointerException(); + } + tasks.add(task); + } + + @Override + public void desync(TaskError error) { + System.out.println("Desync. This is an error. Did the server lag?"); + System.out.println("Error: " + error); + Thread.dumpStack(); + + if (!lastErrors.contains(error)) { + AIChatController.addChatLine("Error: " + error.getMessage()); + } + if (lastErrors.size() > 2) { + lastErrors.removeFirst(); + } + lastErrors.addLast(error); + desync = true; + } + + @Override + 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); + if (tasks.get(i).getClass() + .isAnnotationPresent(SkipWhenSearchingPrefetch.class)) { + continue; + } else if (task instanceof CanPrefaceAndDestroy) { + final CanPrefaceAndDestroy dTask = (CanPrefaceAndDestroy) task; + final List positions = dTask + .getPredestroyPositions(temporaryHelper); + for (final Pos pos : positions) { + if (!temporaryHelper.isAirBlock(pos.x, pos.y, pos.z)) { + temporaryHelper.faceAndDestroy(pos.x, pos.y, pos.z); + found = true; + break; + } + } + System.out.println("Prefacing: " + found + " for " + positions); + } else { + System.out.println("Prefetching showstopper: " + task); + break; + } + } + if (!found) { + System.out.println("Could not prefetch anything. " + tasks.size()); + } + return found; + } + + @Override + protected TickResult onGameTick(AIHelper helper) { + if (desync) { + tasks.clear(); + desync = false; + return TickResult.TICK_AGAIN; + } + + if (searchNewTasks) { + searchTasks(helper); + if (tasks.isEmpty()) { + System.out.println("No more tasks found."); + return TickResult.NO_MORE_WORK; + } + taskTimeout = 0; + searchNewTasks = false; + } else if (tasks.isEmpty()) { + searchNewTasks = true; + return TickResult.TICK_AGAIN; + } + + final AITask task = tasks.peekFirst(); + if (task.isFinished(helper)) { + System.out.println("Task done."); + tasks.removeFirst(); + taskTimeout = 0; + return TickResult.TICK_AGAIN; + } else if (taskTimeout > task.getGameTickTimeout()) { + desync(new StringTaskError("Task timed out.")); + return TickResult.TICK_AGAIN; + } else { + temporaryHelper = helper; + task.runTick(helper, this); + temporaryHelper = null; + taskTimeout++; + return TickResult.TICK_HANDLED; + } + } + + /** + * Searches for tasks to do. Always called in a game tick. The tasks should + * be added with {@link AIHelper#addTask(AITask)}. + *

+ * You cannot make any assumptions on the state of the world. Tasks may even + * have been interrupted in between. You can be sure that there are no tasks + * currently added when this method is called. + * + * @param helper + * The helper that can be used. + */ + protected abstract void searchTasks(AIHelper helper); +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/TintStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/TintStrategy.java index ceea81de..5fbdbe3d 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/TintStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/TintStrategy.java @@ -1,14 +1,12 @@ package net.famzangl.minecraft.minebot.ai.strategy; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.ItemFilter; import net.famzangl.minecraft.minebot.ai.selectors.AndSelector; import net.famzangl.minecraft.minebot.ai.selectors.ColorSelector; import net.famzangl.minecraft.minebot.ai.selectors.NotSelector; import net.famzangl.minecraft.minebot.ai.selectors.OrSelector; import net.famzangl.minecraft.minebot.ai.selectors.OwnTameableSelector; -import net.famzangl.minecraft.minebot.ai.task.AITask; import net.famzangl.minecraft.minebot.ai.task.FaceAndInteractTask; import net.minecraft.client.entity.EntityClientPlayerMP; import net.minecraft.command.IEntitySelector; @@ -18,7 +16,7 @@ import net.minecraft.item.ItemDye; import net.minecraft.item.ItemStack; -public class TintStrategy implements AIStrategy { +public class TintStrategy extends TaskStrategy { private static final int DISTANCE = 20; private final int color; private final int current; @@ -79,9 +77,10 @@ public void searchTasks(AIHelper helper) { } final EntityClientPlayerMP owner = helper.getMinecraft().thePlayer; - int holdingColor = 15 - owner.inventory.getCurrentItem().getItemDamage(); - IEntitySelector wolfSelector = new WolfSelector(owner); - IEntitySelector sheepSelector = new SheepSelector(); + final int holdingColor = 15 - owner.inventory.getCurrentItem() + .getItemDamage(); + final IEntitySelector wolfSelector = new WolfSelector(owner); + final IEntitySelector sheepSelector = new SheepSelector(); IEntitySelector selector; switch (type) { @@ -103,7 +102,7 @@ public void searchTasks(AIHelper helper) { final Entity found = helper.getClosestEntity(DISTANCE, selector); if (found != null) { - helper.addTask(new FaceAndInteractTask(found, selector)); + addTask(new FaceAndInteractTask(found, selector)); } } @@ -112,17 +111,10 @@ public String getDescription() { return "Tinting..."; } - @Override - public AITask getOverrideTask(AIHelper helper) { - return null; - } - @Override public String toString() { return "TintStrategy [color=" + color + ", current=" + current + ", type=" + type + "]"; } - - } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/ValueActionStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/ValueActionStrategy.java new file mode 100644 index 00000000..9c9a72a5 --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/strategy/ValueActionStrategy.java @@ -0,0 +1,99 @@ +package net.famzangl.minecraft.minebot.ai.strategy; + +import net.famzangl.minecraft.minebot.MinebotSettings; +import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.commands.CommandRun; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiMainMenu; +import net.minecraft.client.multiplayer.WorldClient; + +/** + * This stategy takes over whenever an event happens. + * + * @author michael + * + */ +public abstract class ValueActionStrategy extends AIStrategy { + + private double lastValue = -1; + private final MinebotSettings settings; + private boolean shouldLogOut; + private boolean shouldRunCommand; + private boolean shouldStop; + private static final float EPSILON = 0.01f; + + public ValueActionStrategy() { + settings = new MinebotSettings(); + } + + @Override + public boolean takesOverAnyTime() { + return true; + } + + @Override + public boolean checkShouldTakeOver(AIHelper helper) { + final double currentValue = getValue(helper); + if (passedTh(lastValue, currentValue, getSetting("stop"))) { + shouldStop = true; + } else if (passedTh(lastValue, currentValue, getSetting("logout"))) { + shouldLogOut = true; + } else if (passedTh(lastValue, currentValue, getSetting("command"))) { + shouldRunCommand = true; + } + + lastValue = currentValue; + return shouldLogOut || shouldStop || shouldRunCommand; + } + + private boolean passedTh(double lastValue, double currentValue, double value) { + final double v = value - EPSILON; + return lastValue > v && currentValue <= v; + } + + private double getSetting(String string) { + return settings.getFloat(getSettingPrefix() + string + "_value", -1); + } + + protected abstract double getValue(AIHelper helper); + + protected abstract String getSettingPrefix(); + + @Override + protected TickResult onGameTick(AIHelper helper) { + if (shouldRunCommand) { + CommandRun.runCommand(helper, + settings.get(getSettingPrefix() + "_command", "")); + shouldRunCommand = false; + return TickResult.TICK_AGAIN; + } else if (shouldLogOut) { + final Minecraft mc = helper.getMinecraft(); + + mc.theWorld.sendQuittingDisconnectingPacket(); + mc.loadWorld((WorldClient) null); + mc.displayGuiScreen(new GuiMainMenu()); + shouldLogOut = false; + return TickResult.TICK_HANDLED; + } else if (shouldStop) { + // Freeze here. + return TickResult.TICK_HANDLED; + } else { + return TickResult.NO_MORE_WORK; + } + } + + @Override + public String getDescription() { + return shouldStop ? "Stopped because of event." : "Handling event."; + } + + public static AIStrategy makeSafe(AIStrategy baseStrategy) { + final StrategyStack stack = new StrategyStack(); + stack.addStrategy(new DamageTakenStrategy()); + stack.addStrategy(new PlayerComesActionStrategy()); + stack.addStrategy(new CreeperComesActionStrategy()); + stack.addStrategy(baseStrategy); + return new StackStrategy(stack); + } + +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/AITask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/AITask.java index 79a23bf5..649ecccc 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/AITask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/AITask.java @@ -1,13 +1,14 @@ package net.famzangl.minecraft.minebot.ai.task; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; public abstract class AITask { public abstract boolean isFinished(AIHelper h); - public abstract void runTick(AIHelper h); - + public abstract void runTick(AIHelper h, TaskOperations o); + public int getGameTickTimeout() { return 20 * 5; } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/BlockSide.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/BlockSide.java index 2505ce38..fed651a9 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/BlockSide.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/BlockSide.java @@ -1,5 +1,7 @@ package net.famzangl.minecraft.minebot.ai.task; public enum BlockSide { - UPPER_HALF, LOWER_HALF, ANY + UPPER_HALF, + LOWER_HALF, + ANY } \ No newline at end of file diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/CanPrefaceAndDestroy.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/CanPrefaceAndDestroy.java index 0207de02..4e124e01 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/CanPrefaceAndDestroy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/CanPrefaceAndDestroy.java @@ -10,7 +10,9 @@ public interface CanPrefaceAndDestroy { /** * Return a list that this task faces and destroys that could already be * mined before arriving at the target location. - * @param helper TODO + * + * @param helper + * TODO * * @return */ diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/DestroyInRangeTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/DestroyInRangeTask.java index f8b98f27..2f03faea 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/DestroyInRangeTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/DestroyInRangeTask.java @@ -7,6 +7,7 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; public class DestroyInRangeTask extends AITask implements CanPrefaceAndDestroy { @@ -27,7 +28,7 @@ private Pos getNextToDestruct(AIHelper h) { for (int x = minPos.x; x <= maxPos.x; x++) { for (int y = minPos.y; y <= maxPos.y; y++) { for (int z = minPos.z; z <= maxPos.z; z++) { - double rating = rate(h, x, y, z); + final double rating = rate(h, x, y, z); if (rating >= 0 && rating < currentMin) { next = new Pos(x, y, z); currentMin = rating; @@ -73,7 +74,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { Pos n = getNextToDestruct(h); if (facingAttempts > 20) { blacklist.add(n); @@ -110,7 +111,8 @@ public void blacklist(Pos pos) { @Override public List getPredestroyPositions(AIHelper helper) { - Pos next = getNextToDestruct(helper); - return next != null ? Arrays.asList(next) : Collections.emptyList(); + final Pos next = getNextToDestruct(helper); + return next != null ? Arrays.asList(next) : Collections + . emptyList(); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/DoFishTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/DoFishTask.java index fca63f92..a2e48abb 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/DoFishTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/DoFishTask.java @@ -3,6 +3,7 @@ import java.lang.reflect.Field; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.minecraft.client.multiplayer.PlayerControllerMP; import net.minecraft.client.network.NetHandlerPlayClient; import net.minecraft.entity.projectile.EntityFishHook; @@ -86,7 +87,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { if (fishIsCaptured(h)) { h.overrideUseItem(); revoked = true; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/FaceAndInteractTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/FaceAndInteractTask.java index 58686894..e676a897 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/FaceAndInteractTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/FaceAndInteractTask.java @@ -1,6 +1,7 @@ package net.famzangl.minecraft.minebot.ai.task; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.minecraft.command.IEntitySelector; import net.minecraft.entity.Entity; import net.minecraft.entity.item.EntityItem; @@ -11,7 +12,7 @@ public class FaceAndInteractTask extends AITask { - private boolean interacted = false; + protected boolean interacted = false; private final Entity preferedAnimal; private final IEntitySelector alsoAcceptedAnimal; private final boolean doRightClick; @@ -38,7 +39,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { final MovingObjectPosition over = h.getObjectMouseOver(); if (over != null && over.typeOfHit == MovingObjectType.ENTITY && alsoAcceptedAnimal.isEntityApplicable(over.entityHit)) { @@ -73,6 +74,5 @@ public String toString() { + ", alsoAcceptedAnimal=" + alsoAcceptedAnimal + ", doRightClick=" + doRightClick + "]"; } - - + } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/GetOnHotBarTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/GetOnHotBarTask.java index 7fd09a18..6c9b5502 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/GetOnHotBarTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/GetOnHotBarTask.java @@ -2,6 +2,8 @@ import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.ItemFilter; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; +import net.famzangl.minecraft.minebot.ai.task.error.SelectTaskError; import net.minecraft.client.entity.EntityClientPlayerMP; import net.minecraft.client.gui.inventory.GuiInventory; import net.minecraft.client.multiplayer.PlayerControllerMP; @@ -32,7 +34,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { if (h.getMinecraft().currentScreen instanceof GuiInventory) { final GuiInventory screen = (GuiInventory) h.getMinecraft().currentScreen; for (int i = 9; i < 9 * 4; i++) { @@ -58,8 +60,7 @@ public void runTick(AIHelper h) { new GuiInventory(h.getMinecraft().thePlayer)); inventoryOpened = true; } else { - h.buildManager.missingItem(itemFiler); - h.desync(); + o.desync(new SelectTaskError(itemFiler)); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/LogOutTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/LogOutTask.java deleted file mode 100644 index 1d247f86..00000000 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/LogOutTask.java +++ /dev/null @@ -1,29 +0,0 @@ -package net.famzangl.minecraft.minebot.ai.task; - -import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiMainMenu; -import net.minecraft.client.multiplayer.WorldClient; - -public class LogOutTask extends AITask { - - private boolean send; - - @Override - public boolean isFinished(AIHelper h) { - return send; - } - - @Override - public void runTick(AIHelper h) { - if (!send) { - final Minecraft mc = h.getMinecraft(); - - mc.theWorld.sendQuittingDisconnectingPacket(); - mc.loadWorld((WorldClient) null); - mc.displayGuiScreen(new GuiMainMenu()); - send = true; - } - } - -} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/MineBlockTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/MineBlockTask.java index 4ed8693d..1392bdf2 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/MineBlockTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/MineBlockTask.java @@ -1,6 +1,7 @@ package net.famzangl.minecraft.minebot.ai.task; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; public class MineBlockTask extends AITask { private final int x; @@ -19,7 +20,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { h.faceAndDestroy(x, y, z); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/PlaceBlockTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/PlaceBlockTask.java index d99b90f5..5bfecca9 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/PlaceBlockTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/PlaceBlockTask.java @@ -3,6 +3,8 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.BlockItemFilter; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; +import net.famzangl.minecraft.minebot.ai.task.error.SelectTaskError; import net.minecraft.block.Block; import net.minecraftforge.common.util.ForgeDirection; @@ -33,9 +35,10 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { - if (!h.selectCurrentItem(new BlockItemFilter(block))) { - h.desync(); + public void runTick(AIHelper h, TaskOperations o) { + final BlockItemFilter f = new BlockItemFilter(block); + if (!h.selectCurrentItem(f)) { + o.desync(new SelectTaskError(f)); } h.faceSideOf(placeOn.x, placeOn.y, placeOn.z, onSide); diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/PlaceTorchSomewhereTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/PlaceTorchSomewhereTask.java index 01cad32d..ba15e02e 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/PlaceTorchSomewhereTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/PlaceTorchSomewhereTask.java @@ -1,11 +1,14 @@ package net.famzangl.minecraft.minebot.ai.task; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.BlockItemFilter; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; +import net.famzangl.minecraft.minebot.ai.task.error.SelectTaskError; import net.minecraft.block.Block; import net.minecraft.init.Blocks; import net.minecraftforge.common.util.ForgeDirection; @@ -47,8 +50,11 @@ public String toString() { /** * Create a new task- - * @param positions The positions on which the torch should be placed. - * @param preferedDirectionThe direction in which the stick of the torch should be mounted. + * + * @param positions + * The positions on which the torch should be placed. + * @param preferedDirectionThe + * direction in which the stick of the torch should be mounted. */ public PlaceTorchSomewhereTask(List positions, ForgeDirection... preferedDirection) { @@ -59,25 +65,25 @@ public PlaceTorchSomewhereTask(List positions, @Override public boolean isFinished(AIHelper h) { - PosAndDir place = getNextPlace(h); - return place == null || (lastAttempt != null && Block - .isEqualTo(h.getBlock(lastAttempt), Blocks.torch)); + final PosAndDir place = getNextPlace(h); + return place == null || lastAttempt != null + && Block.isEqualTo(h.getBlock(lastAttempt), Blocks.torch); } private PosAndDir getNextPlace(AIHelper h) { if (attemptOnPositions == null) { attemptOnPositions = new LinkedList(); - for (Pos p : places) { - for (ForgeDirection d : - preferedDirection) { - PosAndDir current = new PosAndDir(p, d); - Pos placeOn = current.getPlaceOn(); + for (final Pos p : places) { + for (final ForgeDirection d : preferedDirection) { + final PosAndDir current = new PosAndDir(p, d); + final Pos placeOn = current.getPlaceOn(); if (!h.isAirBlock(placeOn.x, placeOn.y, placeOn.z)) { attemptOnPositions.add(current); } } } - System.out.println("Placing torch somewhere there: " + attemptOnPositions); + System.out.println("Placing torch somewhere there: " + + attemptOnPositions); } while (!attemptOnPositions.isEmpty() @@ -89,13 +95,14 @@ private PosAndDir getNextPlace(AIHelper h) { } @Override - public void runTick(AIHelper h) { - if (!h.selectCurrentItem(new BlockItemFilter(Blocks.torch))) { - h.desync(); + public void runTick(AIHelper h, TaskOperations o) { + final BlockItemFilter f = new BlockItemFilter(Blocks.torch); + if (!h.selectCurrentItem(f)) { + o.desync(new SelectTaskError(f)); } - PosAndDir next = getNextPlace(h); - Pos placeOn = next.getPlaceOn(); + final PosAndDir next = getNextPlace(h); + final Pos placeOn = next.getPlaceOn(); h.faceSideOf(placeOn.x, placeOn.y, placeOn.z, next.dir.getOpposite()); if (h.isFacingBlock(placeOn.x, placeOn.y, placeOn.z, next.dir.getOpposite())) { @@ -113,9 +120,8 @@ public int getGameTickTimeout() { @Override public String toString() { return "PlaceTorchSomewhereTask [places=" + places - + ", preferedDirection=" + preferedDirection + ", lastAttempt=" - + lastAttempt + "]"; + + ", preferedDirection=" + Arrays.toString(preferedDirection) + + "]"; } - - + } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/SendCommandTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/SendCommandTask.java deleted file mode 100644 index 5b6c74e6..00000000 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/SendCommandTask.java +++ /dev/null @@ -1,41 +0,0 @@ -package net.famzangl.minecraft.minebot.ai.task; - -import java.util.Arrays; -import java.util.List; - -import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.minecraft.client.gui.GuiChat; -import net.minecraft.client.gui.GuiScreen; - -@SkipWhenSearchingPrefetch -public class SendCommandTask extends AITask { - private final List commands; - private boolean send; - - public SendCommandTask(List commands) { - super(); - this.commands = commands; - } - - public SendCommandTask(String string) { - this(Arrays.asList(string)); - } - - @Override - public boolean isFinished(AIHelper h) { - return send; - } - - @Override - public void runTick(AIHelper h) { - if (!send && h.getMinecraft().ingameGUI.getChatGUI() != null) { - final GuiChat chat = new GuiChat(); - h.getMinecraft().displayGuiScreen(chat); - for (final String command : commands) { - chat.func_146403_a(command); - } - h.getMinecraft().displayGuiScreen((GuiScreen) null); - send = true; - } - } -} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/UseItemTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/UseItemTask.java index a3cf778f..5d1059f3 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/UseItemTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/UseItemTask.java @@ -2,6 +2,7 @@ import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.ItemFilter; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.minecraft.util.MovingObjectPosition; public class UseItemTask extends AITask { @@ -22,7 +23,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { if (!clicked) { if (filter != null) { if (!h.selectCurrentItem(filter)) { diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/WaitTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/WaitTask.java index f38e81b4..092e085f 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/WaitTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/WaitTask.java @@ -1,19 +1,34 @@ package net.famzangl.minecraft.minebot.ai.task; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; @SkipWhenSearchingPrefetch public class WaitTask extends AITask { - public static WaitTask instance = new WaitTask(); + private int ticks; + + public WaitTask() { + this(1); + } + + public WaitTask(int ticks) { + this.ticks = ticks; + } @Override public boolean isFinished(AIHelper h) { - return true; + return ticks <= 0; + } + + @Override + public void runTick(AIHelper h, TaskOperations o) { + ticks--; } @Override - public void runTick(AIHelper h) { + public String toString() { + return "WaitTask [ticks=" + ticks + "]"; } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/error/PositionTaskError.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/error/PositionTaskError.java new file mode 100644 index 00000000..5f11e5eb --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/error/PositionTaskError.java @@ -0,0 +1,49 @@ +package net.famzangl.minecraft.minebot.ai.task.error; + +import net.famzangl.minecraft.minebot.Pos; + +public class PositionTaskError extends TaskError { + + private final Pos expectedPosition; + + protected PositionTaskError(Pos expectedPosition) { + super("Not standing on: " + expectedPosition); + this.expectedPosition = expectedPosition; + } + + public PositionTaskError(int x, int y, int z) { + this(new Pos(x, y, z)); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + (expectedPosition == null ? 0 : expectedPosition.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final PositionTaskError other = (PositionTaskError) obj; + if (expectedPosition == null) { + if (other.expectedPosition != null) { + return false; + } + } else if (!expectedPosition.equals(other.expectedPosition)) { + return false; + } + return true; + } + +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/error/SelectTaskError.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/error/SelectTaskError.java new file mode 100644 index 00000000..346ac38e --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/error/SelectTaskError.java @@ -0,0 +1,43 @@ +package net.famzangl.minecraft.minebot.ai.task.error; + +import net.famzangl.minecraft.minebot.ai.ItemFilter; + +public final class SelectTaskError extends TaskError { + private final ItemFilter filter; + + public SelectTaskError(ItemFilter filter) { + super("Cannot select: " + filter); + this.filter = filter; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (filter == null ? 0 : filter.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final SelectTaskError other = (SelectTaskError) obj; + if (filter == null) { + if (other.filter != null) { + return false; + } + } else if (!filter.equals(other.filter)) { + return false; + } + return true; + } + +} \ No newline at end of file diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/error/StringTaskError.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/error/StringTaskError.java new file mode 100644 index 00000000..1eaa3943 --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/error/StringTaskError.java @@ -0,0 +1,42 @@ +package net.famzangl.minecraft.minebot.ai.task.error; + +public class StringTaskError extends TaskError { + + private final String message; + + public StringTaskError(String message) { + super(message); + this.message = message; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (message == null ? 0 : message.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final StringTaskError other = (StringTaskError) obj; + if (message == null) { + if (other.message != null) { + return false; + } + } else if (!message.equals(other.message)) { + return false; + } + return true; + } + +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/error/TaskError.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/error/TaskError.java new file mode 100644 index 00000000..49b1421d --- /dev/null +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/error/TaskError.java @@ -0,0 +1,14 @@ +package net.famzangl.minecraft.minebot.ai.task.error; + +public class TaskError { + private final String message; + + protected TaskError(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + +} diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/AlignToGridTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/AlignToGridTask.java index 33cdb697..5bcef44e 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/AlignToGridTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/AlignToGridTask.java @@ -2,13 +2,15 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; import net.famzangl.minecraft.minebot.ai.task.SkipWhenSearchingPrefetch; /** * Ensures that the player is standing on a block. + * * @author michael - * + * */ @SkipWhenSearchingPrefetch public class AlignToGridTask extends AITask { @@ -32,8 +34,8 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { - h.walkTowards(x + 0.5, z + 0.5, false, !h.faceAndDestroyForNextTask()); + public void runTick(AIHelper h, TaskOperations o) { + h.walkTowards(x + 0.5, z + 0.5, false, !o.faceAndDestroyForNextTask()); } @Override diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/DownwardsMoveTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/DownwardsMoveTask.java index aead912f..bf7fcfaa 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/DownwardsMoveTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/DownwardsMoveTask.java @@ -1,7 +1,9 @@ package net.famzangl.minecraft.minebot.ai.task.move; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; +import net.famzangl.minecraft.minebot.ai.task.error.PositionTaskError; public class DownwardsMoveTask extends AITask { private final int x; @@ -20,14 +22,14 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { if (!h.isAirBlock(x, y + 1, z)) { // grass, ... h.faceAndDestroy(x, y + 1, z); } else if (!h.isAirBlock(x, y, z)) { if (!h.isStandingOn(x, y + 1, z)) { System.out.println("Not standing on the right block."); - h.desync(); + o.desync(new PositionTaskError(x, y + 1, z)); } h.faceAndDestroy(x, y, z); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/HorizontalMoveTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/HorizontalMoveTask.java index 2c804fb4..bf066dd3 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/HorizontalMoveTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/HorizontalMoveTask.java @@ -5,6 +5,7 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; import net.famzangl.minecraft.minebot.ai.task.CanPrefaceAndDestroy; @@ -25,13 +26,13 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { if (!h.isAirBlock(x, y + 1, z)) { h.faceAndDestroy(x, y + 1, z); } else if (!h.isAirBlock(x, y, z) && !h.canWalkOn(h.getBlock(x, y, z))) { h.faceAndDestroy(x, y, z); } else { - boolean nextIsFacing = h.faceAndDestroyForNextTask(); + final boolean nextIsFacing = o.faceAndDestroyForNextTask(); h.walkTowards(x + 0.5, z + 0.5, doJump(h), !nextIsFacing); } } @@ -47,7 +48,7 @@ public String toString() { @Override public List getPredestroyPositions(AIHelper helper) { - ArrayList arrayList = new ArrayList(); + final ArrayList arrayList = new ArrayList(); arrayList.add(new Pos(x, y + 1, z)); if (!helper.canWalkOn(helper.getBlock(x, y, z))) { arrayList.add(new Pos(x, y, z)); diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/JumpMoveTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/JumpMoveTask.java index 5a053e3f..4fb5652f 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/JumpMoveTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/JumpMoveTask.java @@ -4,6 +4,7 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; public class JumpMoveTask extends HorizontalMoveTask { @@ -18,17 +19,17 @@ public JumpMoveTask(int x, int y, int z, int oldX, int oldZ) { @Override protected boolean doJump(AIHelper h) { -// Pos player = h.getPlayerPosition(); -// return player.x != x && player.z != z; + // Pos player = h.getPlayerPosition(); + // return player.x != x && player.z != z; return true; } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { if (!h.isAirBlock(oldX, y + 1, oldZ)) { h.faceAndDestroy(oldX, y + 1, oldZ); } else { - super.runTick(h); + super.runTick(h, o); } } @@ -40,7 +41,7 @@ public String toString() { @Override public List getPredestroyPositions(AIHelper helper) { - List list = super.getPredestroyPositions(helper); + final List list = super.getPredestroyPositions(helper); list.add(0, new Pos(oldX, y + 1, oldZ)); return list; } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/SneakTowardsTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/SneakTowardsTask.java index c43f5b2c..ad03573c 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/SneakTowardsTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/SneakTowardsTask.java @@ -1,6 +1,7 @@ package net.famzangl.minecraft.minebot.ai.task.move; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; import net.minecraftforge.common.util.ForgeDirection; @@ -30,7 +31,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { // TODO Auto-generated method stub } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/UpwardsMoveTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/UpwardsMoveTask.java index 3c49b525..fc308b3c 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/UpwardsMoveTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/move/UpwardsMoveTask.java @@ -2,6 +2,8 @@ import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.ItemFilter; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; +import net.famzangl.minecraft.minebot.ai.task.error.PositionTaskError; import net.famzangl.minecraft.minebot.ai.task.place.JumpingPlaceBlockAtFloorTask; public class UpwardsMoveTask extends JumpingPlaceBlockAtFloorTask { @@ -10,15 +12,14 @@ public UpwardsMoveTask(int x, int y, int z, ItemFilter filter) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { if (!h.isAirBlock(x, y + 1, z)) { if (!h.isStandingOn(x, y - 1, z)) { - System.out.println("Not standing on the right block."); - h.desync(); + o.desync(new PositionTaskError(x, y - 1, z)); } h.faceAndDestroy(x, y + 1, z); } else { - super.runTick(h); + super.runTick(h, o); } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/DestroyBlockTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/DestroyBlockTask.java index 5c851f21..2c6f2e1b 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/DestroyBlockTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/DestroyBlockTask.java @@ -1,6 +1,7 @@ package net.famzangl.minecraft.minebot.ai.task.place; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; public class DestroyBlockTask extends AITask { @@ -20,7 +21,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { h.faceAndDestroy(x, y, z); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/JumpingPlaceAtHalfTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/JumpingPlaceAtHalfTask.java index 58e7561a..104f7485 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/JumpingPlaceAtHalfTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/JumpingPlaceAtHalfTask.java @@ -2,7 +2,9 @@ import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.ItemFilter; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.BlockSide; +import net.famzangl.minecraft.minebot.ai.task.error.StringTaskError; import net.minecraftforge.common.util.ForgeDirection; public class JumpingPlaceAtHalfTask extends JumpingPlaceBlockAtFloorTask { @@ -27,14 +29,14 @@ public JumpingPlaceAtHalfTask(int x, int y, int z, ItemFilter filter, } @Override - protected void faceBlock(AIHelper h) { + protected void faceBlock(AIHelper h, TaskOperations o) { final ForgeDirection[] dirs = getBuildDirs(); for (int i = 0; i < dirs.length; i++) { if (faceSideBlock(h, dirs[attempts++ % dirs.length])) { return; } } - h.desync(); + o.desync(new StringTaskError("Could not face anywhere to place.")); } protected ForgeDirection[] getBuildDirs() { diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/PlaceBlockAtFloorTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/PlaceBlockAtFloorTask.java index 5e4b63e2..1d73866b 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/PlaceBlockAtFloorTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/PlaceBlockAtFloorTask.java @@ -2,7 +2,9 @@ import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.ItemFilter; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; +import net.famzangl.minecraft.minebot.ai.task.error.SelectTaskError; import net.minecraftforge.common.util.ForgeDirection; public class PlaceBlockAtFloorTask extends AITask { @@ -29,17 +31,16 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { if (faceTimer > 0) { faceTimer--; } if (h.isAirBlock(x, getPlaceAtY(), z)) { if (!h.selectCurrentItem(filter)) { - System.out.println("Cannot select " + filter); - h.desync(); + o.desync(new SelectTaskError(filter)); } else { if (faceTimer == 0) { - faceBlock(h); + faceBlock(h, o); faceTimer = 2; } else { tryPlaceBlock(h); @@ -48,7 +49,7 @@ public void runTick(AIHelper h) { } } - protected void faceBlock(AIHelper h) { + protected void faceBlock(AIHelper h, TaskOperations o) { h.faceSideOf(x, getPlaceAtY() - 1, z, ForgeDirection.UP); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/SneakAndPlaceAtHalfTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/SneakAndPlaceAtHalfTask.java index 5ede0d51..a028fad9 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/SneakAndPlaceAtHalfTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/SneakAndPlaceAtHalfTask.java @@ -3,7 +3,9 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.BlockItemFilter; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.BlockSide; +import net.famzangl.minecraft.minebot.ai.task.error.StringTaskError; import net.minecraftforge.common.util.ForgeDirection; public class SneakAndPlaceAtHalfTask extends SneakAndPlaceTask { @@ -29,7 +31,7 @@ public SneakAndPlaceAtHalfTask(int x, int y, int z, BlockItemFilter filter, } @Override - protected void faceBlock(AIHelper h) { + protected void faceBlock(AIHelper h, TaskOperations o) { final ForgeDirection[] dirs = getBuildDirs(); attempts++; for (int i = 0; i < dirs.length; i++) { @@ -41,7 +43,7 @@ protected void faceBlock(AIHelper h) { attempts += 10; } } - h.desync(); + o.desync(new StringTaskError("Could not face anywhere to place.")); } private void faceSideBlock(AIHelper h, ForgeDirection useSide) { diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/SneakAndPlaceAtSideTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/SneakAndPlaceAtSideTask.java index 99bd14f3..78b8202b 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/SneakAndPlaceAtSideTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/SneakAndPlaceAtSideTask.java @@ -17,10 +17,9 @@ public SneakAndPlaceAtSideTask(int x, int y, int z, BlockItemFilter filter, @Override protected ForgeDirection[] getBuildDirs() { - return new ForgeDirection[] { - lookingDirection, + return new ForgeDirection[] { lookingDirection, lookingDirection.getRotation(ForgeDirection.UP), - lookingDirection.getRotation(ForgeDirection.DOWN)}; + lookingDirection.getRotation(ForgeDirection.DOWN) }; } @Override diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/SneakAndPlaceTask.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/SneakAndPlaceTask.java index 60d36ba6..e3001b53 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/SneakAndPlaceTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/place/SneakAndPlaceTask.java @@ -3,7 +3,9 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.BlockItemFilter; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; +import net.famzangl.minecraft.minebot.ai.task.error.SelectTaskError; import net.minecraft.util.MovementInput; import net.minecraftforge.common.util.ForgeDirection; @@ -45,7 +47,8 @@ public SneakAndPlaceTask(int x, int y, int z, BlockItemFilter filter, this.filter = filter; this.relativeFrom = relativeFrom; this.minBuildHeight = minBuildHeight; - ForgeDirection foundInDir = AIHelper.getDirectionForXZ(-relativeFrom.x, -relativeFrom.z); + final ForgeDirection foundInDir = AIHelper.getDirectionForXZ( + -relativeFrom.x, -relativeFrom.z); if (relativeFrom.y != 1 || foundInDir == null) { throw new IllegalArgumentException(); } @@ -58,7 +61,7 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { if (faceTimer > 0) { faceTimer--; } @@ -67,14 +70,13 @@ public void runTick(AIHelper h) { final boolean hasRequiredHeight = h.getMinecraft().thePlayer.boundingBox.minY > minBuildHeight - 0.05; if (hasRequiredHeight) { if (faceTimer == 0) { - faceBlock(h); + faceBlock(h, o); faceTimer = 3; } else if (isFacingRightBlock(h)) { if (h.selectCurrentItem(filter)) { h.overrideUseItem(); } else { - System.out.println("Cannot select " + filter); - h.desync(); + o.desync(new SelectTaskError(filter)); } } } else { @@ -90,7 +92,7 @@ protected boolean isFacingRightBlock(AIHelper h) { inDirection); } - protected void faceBlock(AIHelper h) { + protected void faceBlock(AIHelper h, TaskOperations o) { h.faceSideOf(x + relativeFrom.x, y - 1, z + relativeFrom.z, inDirection); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/BuildManager.java b/Minebot/src/net/famzangl/minecraft/minebot/build/BuildManager.java index 62a12fd0..7f19a306 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/BuildManager.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/BuildManager.java @@ -7,9 +7,7 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.ItemFilter; -import net.famzangl.minecraft.minebot.ai.command.AIChatController; import net.famzangl.minecraft.minebot.build.blockbuild.BuildTask; -import net.famzangl.minecraft.minebot.build.commands.CommandCount; public class BuildManager { @@ -44,10 +42,11 @@ public List getScheduled() { return Collections.unmodifiableList(buildTasks); } - public void missingItem(ItemFilter itemFiler) { - if (itemFiler != lastMissing) { - AIChatController.addChatLine("Cannot handle missing item: " + CommandCount.niceFilterName(itemFiler)); - } - lastMissing = itemFiler; - } + // public void missingItem(ItemFilter itemFiler) { + // if (itemFiler != lastMissing) { + // AIChatController.addChatLine("Cannot handle missing item: " + + // CommandCount.niceFilterName(itemFiler)); + // } + // lastMissing = itemFiler; + // } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/ForBuildPathFinder.java b/Minebot/src/net/famzangl/minecraft/minebot/build/ForBuildPathFinder.java index 9c51e405..db97daba 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/ForBuildPathFinder.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/ForBuildPathFinder.java @@ -25,8 +25,7 @@ public class ForBuildPathFinder extends MovePathFinder { private final boolean canBuildUp; private boolean noPathFound; - public ForBuildPathFinder(AIHelper helper, BuildTask task) { - super(helper); + public ForBuildPathFinder(BuildTask task) { canBuildUp = helper.canSelectItem(new BlockItemFilter(Blocks.carpet)); this.task = task; } @@ -50,7 +49,7 @@ protected int[] getNeighbours(int currentNode) { private void getNeighbours(int[] fill, int offset, int currentNode, int x, int z) { final int cy = getY(currentNode); - int max = canBuildUp ? 3 : 1; + final int max = canBuildUp ? 3 : 1; for (int y = cy + 1; y <= cy + max; y++) { if (!helper.isAirBlock(getX(currentNode), y + 1, getZ(currentNode))) { break; @@ -101,16 +100,15 @@ protected float rateDestination(int distance, int x, int y, int z) { @Override protected void foundPath(LinkedList path) { Pos currentPos = path.removeFirst(); - helper.addTask(new AlignToGridTask(currentPos.x, currentPos.y, - currentPos.z)); + addTask(new AlignToGridTask(currentPos.x, currentPos.y, currentPos.z)); while (!path.isEmpty()) { final Pos nextPos = path.removeFirst(); - helper.addTask(new WalkTowardsTask(currentPos, nextPos)); + addTask(new WalkTowardsTask(currentPos, nextPos)); currentPos = nextPos; } noPathFound = true; } - + @Override protected void noPathFound() { noPathFound = true; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/LogItemFilter.java b/Minebot/src/net/famzangl/minecraft/minebot/build/LogItemFilter.java index 463c7f3e..3d6d9612 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/LogItemFilter.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/LogItemFilter.java @@ -22,21 +22,25 @@ public boolean matches(ItemStack itemStack) { public int hashCode() { final int prime = 31; int result = super.hashCode(); - result = prime * result + ((logType == null) ? 0 : logType.hashCode()); + result = prime * result + (logType == null ? 0 : logType.hashCode()); return result; } @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (!super.equals(obj)) + } + if (!super.equals(obj)) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; - LogItemFilter other = (LogItemFilter) obj; - if (logType != other.logType) + } + final LogItemFilter other = (LogItemFilter) obj; + if (logType != other.logType) { return false; + } return true; } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/NextTaskTask.java b/Minebot/src/net/famzangl/minecraft/minebot/build/NextTaskTask.java index 28976ed4..d5b8733e 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/NextTaskTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/NextTaskTask.java @@ -1,6 +1,7 @@ package net.famzangl.minecraft.minebot.build; import net.famzangl.minecraft.minebot.ai.AIHelper; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; public final class NextTaskTask extends AITask { @@ -15,7 +16,7 @@ public NextTaskTask(int tasksToSkip) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { while (tasksToSkip > 0) { if (h.buildManager.peekNextTask() != null) { h.buildManager.popNextTask(); diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/WalkTowardsTask.java b/Minebot/src/net/famzangl/minecraft/minebot/build/WalkTowardsTask.java index 2ec9a4b0..713a0543 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/WalkTowardsTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/WalkTowardsTask.java @@ -5,7 +5,9 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; import net.famzangl.minecraft.minebot.ai.BlockItemFilter; +import net.famzangl.minecraft.minebot.ai.strategy.TaskOperations; import net.famzangl.minecraft.minebot.ai.task.AITask; +import net.famzangl.minecraft.minebot.ai.task.error.SelectTaskError; import net.minecraft.init.Blocks; import net.minecraft.util.MovementInput; @@ -35,12 +37,12 @@ public boolean isFinished(AIHelper h) { } @Override - public void runTick(AIHelper h) { + public void runTick(AIHelper h, TaskOperations o) { if (subTask != null && subTask.isFinished(h)) { subTask = null; } if (subTask != null) { - subTask.runTick(h); + subTask.runTick(h, o); } else { final int carpetY = getUpperCarpetY(h); final double carpetBuildHeight = h.realBlockTopY(fromPos.x, @@ -49,14 +51,14 @@ public void runTick(AIHelper h) { nextPos.z); if (carpetBuildHeight < destHeight - 1) { System.out.println("Moving upwards. Carpets are at " + carpetY); - int floorY = Math.max(carpetY, fromPos.y - 1); + final int floorY = Math.max(carpetY, fromPos.y - 1); h.faceBlock(fromPos.x, floorY, fromPos.z); if (h.isFacingBlock(fromPos.x, floorY, fromPos.z, 1)) { if (h.selectCurrentItem(CARPET)) { h.overrideUseItem(); carpets.add(new Pos(fromPos.x, floorY + 1, fromPos.z)); } else { - h.buildManager.missingItem(CARPET); + o.desync(new SelectTaskError(CARPET)); } } final MovementInput i = new MovementInput(); @@ -69,17 +71,17 @@ public void runTick(AIHelper h) { while (!carpets.isEmpty()) { // Clean up carpets we already "lost" - Pos last = carpets.getLast(); + final Pos last = carpets.getLast(); if (h.isAirBlock(last.x, last.y, last.z)) { carpets.removeLast(); } } - int x = fromPos.x - nextPos.x; - int z = fromPos.x - nextPos.x; + final int x = fromPos.x - nextPos.x; + final int z = fromPos.x - nextPos.x; if (h.sneakFrom(nextPos.x, nextPos.y, nextPos.z, AIHelper.getDirectionForXZ(x, z))) { - Pos last = carpets.getLast(); + final Pos last = carpets.getLast(); h.faceAndDestroy(last.x, last.y, last.z); } @@ -99,8 +101,7 @@ public void runTick(AIHelper h) { */ private int getUpperCarpetY(AIHelper h) { int upperCarpet = -1; - for (int y = AIHelper.blockIsOneOf( - h.getBlock(fromPos), Blocks.air, + for (int y = AIHelper.blockIsOneOf(h.getBlock(fromPos), Blocks.air, Blocks.carpet) ? fromPos.y : fromPos.y + 1; y < nextPos.y; y++) { if (AIHelper.blockIsOneOf(h.getBlock(fromPos.x, y, fromPos.z), Blocks.carpet)) { diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/WoodItemFilter.java b/Minebot/src/net/famzangl/minecraft/minebot/build/WoodItemFilter.java index 81d438b9..e4c5427d 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/WoodItemFilter.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/WoodItemFilter.java @@ -23,22 +23,25 @@ public boolean matches(ItemStack itemStack) { public int hashCode() { final int prime = 31; int result = super.hashCode(); - result = prime * result - + ((woodType == null) ? 0 : woodType.hashCode()); + result = prime * result + (woodType == null ? 0 : woodType.hashCode()); return result; } @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (!super.equals(obj)) + } + if (!super.equals(obj)) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; - WoodItemFilter other = (WoodItemFilter) obj; - if (woodType != other.woodType) + } + final WoodItemFilter other = (WoodItemFilter) obj; + if (woodType != other.woodType) { return false; + } return true; } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/WoodType.java b/Minebot/src/net/famzangl/minecraft/minebot/build/WoodType.java index dd468eba..d2225673 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/WoodType.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/WoodType.java @@ -4,8 +4,12 @@ import net.minecraft.init.Blocks; public enum WoodType { - OAK(Blocks.log, 0), SPRUCE(Blocks.log, 1), BIRCH(Blocks.log, 2), JUNGLE( - Blocks.log, 3), ACACIA(Blocks.log2, 0), DARK_OAK(Blocks.log2, 1); + OAK(Blocks.log, 0), + SPRUCE(Blocks.log, 1), + BIRCH(Blocks.log, 2), + JUNGLE(Blocks.log, 3), + ACACIA(Blocks.log2, 0), + DARK_OAK(Blocks.log2, 1); public final Block block; public final int lowerBits; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/BuildTask.java b/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/BuildTask.java index 569c330c..922cc312 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/BuildTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/BuildTask.java @@ -158,10 +158,13 @@ public boolean couldBuildFrom(AIHelper helper, int x, int y, int z) { /** * @param add - * @param rotateSteps 0..3 Steps of ForgeDirection.rotate(UP). - * @param mirror Applied after rotate (if possible); + * @param rotateSteps + * 0..3 Steps of ForgeDirection.rotate(UP). + * @param mirror + * Applied after rotate (if possible); * @return */ - public abstract BuildTask withPositionAndRotation(Pos add, int rotateSteps, MirrorDirection mirror); + public abstract BuildTask withPositionAndRotation(Pos add, int rotateSteps, + MirrorDirection mirror); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/CubeBuildTask.java b/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/CubeBuildTask.java index 38bbbd6c..bfdf9c55 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/CubeBuildTask.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/CubeBuildTask.java @@ -61,5 +61,5 @@ public boolean couldBuildFrom(AIHelper helper, int x, int y, int z) { public ItemFilter getRequiredItem() { return blockFilter; } - + } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/SlabFilter.java b/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/SlabFilter.java index 51862878..1731d084 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/SlabFilter.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/SlabFilter.java @@ -22,24 +22,28 @@ protected boolean matchesItem(ItemStack itemStack, ItemBlock item) { public int hashCode() { final int prime = 31; int result = super.hashCode(); - result = prime * result + ((type == null) ? 0 : type.hashCode()); + result = prime * result + (type == null ? 0 : type.hashCode()); return result; } @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (!super.equals(obj)) + } + if (!super.equals(obj)) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; - SlabFilter other = (SlabFilter) obj; - if (type != other.type) + } + final SlabFilter other = (SlabFilter) obj; + if (type != other.type) { return false; + } return true; } - + @Override public String getDescriptiveString() { return type.toString().toLowerCase() + " slabs"; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/SlabType.java b/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/SlabType.java index 1ec88f61..c94ea646 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/SlabType.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/blockbuild/SlabType.java @@ -4,21 +4,21 @@ import net.minecraft.init.Blocks; public enum SlabType { - STONE(Blocks.stone_slab, Blocks.double_stone_slab, 0), SANDSTONE( - Blocks.stone_slab, Blocks.double_stone_slab, 1), WOODEN( - Blocks.stone_slab, Blocks.double_stone_slab, 2), COBBLESTONE( - Blocks.stone_slab, Blocks.double_stone_slab, 3), BRICK( - Blocks.stone_slab, Blocks.double_stone_slab, 4), STONE_BRICK( - Blocks.stone_slab, Blocks.double_stone_slab, 5), NETHER_BRICK( - Blocks.stone_slab, Blocks.double_stone_slab, 6), QUARTZ( - Blocks.stone_slab, Blocks.double_stone_slab, 7), + STONE(Blocks.stone_slab, Blocks.double_stone_slab, 0), + SANDSTONE(Blocks.stone_slab, Blocks.double_stone_slab, 1), + WOODEN(Blocks.stone_slab, Blocks.double_stone_slab, 2), + COBBLESTONE(Blocks.stone_slab, Blocks.double_stone_slab, 3), + BRICK(Blocks.stone_slab, Blocks.double_stone_slab, 4), + STONE_BRICK(Blocks.stone_slab, Blocks.double_stone_slab, 5), + NETHER_BRICK(Blocks.stone_slab, Blocks.double_stone_slab, 6), + QUARTZ(Blocks.stone_slab, Blocks.double_stone_slab, 7), - OAK(Blocks.wooden_slab, Blocks.double_wooden_slab, 0), SPRUCE( - Blocks.wooden_slab, Blocks.double_wooden_slab, 1), BIRCH( - Blocks.wooden_slab, Blocks.double_wooden_slab, 2), JUNGLE( - Blocks.wooden_slab, Blocks.double_wooden_slab, 3), ACACIA( - Blocks.wooden_slab, Blocks.double_wooden_slab, 4), DARK_OAK( - Blocks.wooden_slab, Blocks.double_wooden_slab, 5); + OAK(Blocks.wooden_slab, Blocks.double_wooden_slab, 0), + SPRUCE(Blocks.wooden_slab, Blocks.double_wooden_slab, 1), + BIRCH(Blocks.wooden_slab, Blocks.double_wooden_slab, 2), + JUNGLE(Blocks.wooden_slab, Blocks.double_wooden_slab, 3), + ACACIA(Blocks.wooden_slab, Blocks.double_wooden_slab, 4), + DARK_OAK(Blocks.wooden_slab, Blocks.double_wooden_slab, 5); public final Block slabBlock; public final Block doubleBlock; public final int meta; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandBuild.java b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandBuild.java index 1cf0bdf3..2f60bafd 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandBuild.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandBuild.java @@ -4,7 +4,6 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AIChatController; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; @@ -12,7 +11,9 @@ import net.famzangl.minecraft.minebot.ai.command.ParameterType; import net.famzangl.minecraft.minebot.ai.render.MarkingStrategy; import net.famzangl.minecraft.minebot.ai.render.PosMarkerRenderer; -import net.famzangl.minecraft.minebot.ai.task.AITask; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.TaskStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; import net.famzangl.minecraft.minebot.ai.task.GetOnHotBarTask; import net.famzangl.minecraft.minebot.ai.task.WaitTask; import net.famzangl.minecraft.minebot.ai.task.move.AlignToGridTask; @@ -24,7 +25,7 @@ @AICommand(helpText = "Runs all tasks that are scheduled for building.", name = "minebuild") public class CommandBuild { - private static final class BuildStrategy implements AIStrategy, + private static final class BuildStrategy extends TaskStrategy implements MarkingStrategy { private boolean alignSend; private ForBuildPathFinder pathFinder; @@ -40,23 +41,23 @@ public void searchTasks(AIHelper helper) { if (task == null) { AIChatController.addChatLine("No more build tasks."); } else if (!alignSend) { - helper.addTask(new AlignToGridTask(helper.getPlayerPosition())); + addTask(new AlignToGridTask(helper.getPlayerPosition())); alignSend = true; } else if ((pos = isAroundSite(helper, task)) == null) { if (pathFinder == null) { - pathFinder = new ForBuildPathFinder(helper, task); + pathFinder = new ForBuildPathFinder(task); } - if (!pathFinder.searchSomethingAround(helper - .getPlayerPosition())) { - helper.addTask(WaitTask.instance); + if (!pathFinder.searchSomethingAround( + helper.getPlayerPosition(), helper, this)) { + addTask(new WaitTask()); } else if (pathFinder.isNoPathFound()) { AIChatController .addChatLine("Cannot navigate to next build task."); } } else { - helper.addTask(new GetOnHotBarTask(task.getRequiredItem())); - helper.addTask(task.getPlaceBlockTask(pos)); - helper.addTask(new NextTaskTask()); + addTask(new GetOnHotBarTask(task.getRequiredItem())); + addTask(task.getPlaceBlockTask(pos)); + addTask(new NextTaskTask()); alignSend = false; pathFinder = null; } @@ -70,11 +71,6 @@ private void reloadPositions(List list) { } } - @Override - public AITask getOverrideTask(AIHelper helper) { - return null; - } - @Override public String getDescription() { return "Building."; @@ -90,7 +86,7 @@ public void drawMarkers(RenderWorldLastEvent event, AIHelper helper) { public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "build", description = "") String nameArg2) { - return new BuildStrategy(); + return ValueActionStrategy.makeSafe(new BuildStrategy()); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandClearArea.java b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandClearArea.java index 37e5be6b..da6d1e23 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandClearArea.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandClearArea.java @@ -1,14 +1,16 @@ package net.famzangl.minecraft.minebot.build.commands; +import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AIChatController; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; import net.famzangl.minecraft.minebot.ai.path.ClearAreaPathfinder; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.PathFinderStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; @AICommand(helpText = "Clears the selected area.", name = "minebuild") public class CommandClearArea { @@ -24,10 +26,11 @@ private ClearAreaStrategy(ClearAreaPathfinder pathFinder) { @Override public void searchTasks(AIHelper helper) { - int max = pathFinder.getAreaSize(); + final int max = pathFinder.getAreaSize(); if (max <= 100000) { - progress = 100 - Math - .round(100f * pathFinder.getToClearCount() / max) + "%"; + progress = 100 + - Math.round(100f * pathFinder.getToClearCount(helper) + / max) + "%"; } super.searchTasks(helper); } @@ -42,11 +45,14 @@ public String getDescription() { public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "clear", description = "") String nameArg) { - if (helper.getPos1() == null || helper.getPos2() == null) { + final Pos pos1 = helper.getPos1(); + final Pos pos2 = helper.getPos2(); + if (pos1 == null || pos2 == null) { AIChatController.addChatLine("Set positions first."); return null; } else { - return new ClearAreaStrategy(new ClearAreaPathfinder(helper)); + return ValueActionStrategy.makeSafe(new ClearAreaStrategy( + new ClearAreaPathfinder(pos1, pos2))); } } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandCount.java b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandCount.java index 826ec24c..f96e6eb1 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandCount.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandCount.java @@ -7,7 +7,6 @@ import java.util.Map.Entry; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.BlockItemFilter; import net.famzangl.minecraft.minebot.ai.ItemFilter; import net.famzangl.minecraft.minebot.ai.command.AIChatController; @@ -15,6 +14,7 @@ import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.build.blockbuild.BuildTask; import com.google.common.base.Function; @@ -27,14 +27,14 @@ public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "count", description = "") String nameArg2) { - Hashtable counts = new Hashtable(); - for (BuildTask s : helper.buildManager.getScheduled()) { - ItemFilter required = s.getRequiredItem(); - Integer count = counts.get(required); + final Hashtable counts = new Hashtable(); + for (final BuildTask s : helper.buildManager.getScheduled()) { + final ItemFilter required = s.getRequiredItem(); + final Integer count = counts.get(required); counts.put(required, count == null ? 1 : count + 1); } - ArrayList> list = new ArrayList>( + final ArrayList> list = new ArrayList>( counts.entrySet()); Collections.sort(list, new Comparator>() { @Override @@ -48,8 +48,8 @@ public int compare(Entry o1, new Function, String>() { @Override public String apply(Entry item) { - ItemFilter filter = item.getKey(); - String key = niceFilterName(filter); + final ItemFilter filter = item.getKey(); + final String key = niceFilterName(filter); return key + ": " + item.getValue(); } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandListBuilds.java b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandListBuilds.java index a13e1c08..8ffb54c6 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandListBuilds.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandListBuilds.java @@ -1,12 +1,12 @@ package net.famzangl.minecraft.minebot.build.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AIChatController; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.build.blockbuild.BuildTask; import com.google.common.base.Function; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandMove.java b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandMove.java index 736f745a..2ce3edeb 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandMove.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandMove.java @@ -5,12 +5,11 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; -import net.famzangl.minecraft.minebot.ai.task.AITask; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.build.blockbuild.BuildTask; import net.famzangl.minecraft.minebot.build.blockbuild.MirrorDirection; import net.minecraftforge.common.util.ForgeDirection; @@ -23,7 +22,7 @@ */ @AICommand(helpText = "Move everything that is scheduled in the direction you are looking / given", name = "minebuild") public class CommandMove { - private static final class MoveStrategy implements AIStrategy { + private static final class MoveStrategy extends AIStrategy { private final Pos relative; private final Rotate rotate; private final MirrorDirection mirror; @@ -36,16 +35,54 @@ private MoveStrategy(Pos relative, Rotate rotate, MirrorDirection mirror) { } @Override - public void searchTasks(AIHelper helper) { + public boolean checkShouldTakeOver(AIHelper helper) { + return !moved; + } + + @Override + protected TickResult onGameTick(AIHelper helper) { if (!moved) { - helper.addTask(new MoveTask(relative, rotate, mirror)); + final ArrayList tasks = new ArrayList(); + final Pos center = getCenter(helper.buildManager.getScheduled()); + while (helper.buildManager.peekNextTask() != null) { + final BuildTask next = helper.buildManager.popNextTask(); + Pos nextPos = next.getForPosition().add(relative); + if (rotate != null) { + nextPos = rotate.apply(nextPos.subtract(center)).add( + center); + } + if (mirror == MirrorDirection.EAST_WEST) { + // mirror on x axes + nextPos = new Pos(center.x * 2 - nextPos.x, nextPos.y, + nextPos.z); + } else if (mirror == MirrorDirection.NORTH_SOUTH) { + nextPos = new Pos(nextPos.x, nextPos.y, center.z * 2 + - nextPos.z); + } + + final BuildTask task = next.withPositionAndRotation( + nextPos, rotate == null ? 0 : rotate.r, mirror); + tasks.add(task); + } + + for (final BuildTask task : tasks) { + helper.buildManager.addTask(task); + } moved = true; } + return TickResult.NO_MORE_WORK; } - @Override - public AITask getOverrideTask(AIHelper helper) { - return null; + private Pos getCenter(List scheduled) { + double x = 0, y = 0, z = 0; + for (final BuildTask s : scheduled) { + final Pos pos = s.getForPosition(); + x += pos.x; + y += pos.y; + z += pos.z; + } + final int c = Math.max(1, scheduled.size()); + return new Pos((int) (x / c), (int) (y / c), (int) (z / c)); } @Override @@ -78,67 +115,6 @@ public Pos apply(Pos current) { } } - private static final class MoveTask extends AITask { - private final Pos relative; - private final Rotate rotate; - private final MirrorDirection mirror; - private boolean moved; - - private MoveTask(Pos relative, Rotate rotate, MirrorDirection mirror) { - this.relative = relative; - this.rotate = rotate; - this.mirror = mirror; - } - - @Override - public void runTick(AIHelper h) { - ArrayList tasks = new ArrayList(); - Pos center = getCenter(h.buildManager.getScheduled()); - while (h.buildManager.peekNextTask() != null) { - BuildTask next = h.buildManager.popNextTask(); - Pos nextPos = next.getForPosition().add(relative); - if (rotate != null) { - nextPos = rotate.apply(nextPos.subtract(center)) - .add(center); - } - if (mirror == MirrorDirection.EAST_WEST) { - // mirror on x axes - nextPos = new Pos(center.x * 2 - nextPos.x, nextPos.y, - nextPos.z); - } else if (mirror == MirrorDirection.NORTH_SOUTH) { - nextPos = new Pos(nextPos.x, nextPos.y, center.z * 2 - - nextPos.z); - } - - BuildTask task = next.withPositionAndRotation(nextPos, - rotate == null ? 0 : rotate.r, mirror); - tasks.add(task); - } - - for (BuildTask task : tasks) { - h.buildManager.addTask(task); - } - moved = true; - } - - private Pos getCenter(List scheduled) { - double x = 0, y = 0, z = 0; - for (BuildTask s : scheduled) { - Pos pos = s.getForPosition(); - x += pos.x; - y += pos.y; - z += pos.z; - } - int c = Math.max(1, scheduled.size()); - return new Pos((int) (x / c), (int) (y / c), (int) (z / c)); - } - - @Override - public boolean isFinished(AIHelper h) { - return moved; - } - } - @AICommandInvocation() public static AIStrategy run( AIHelper helper, diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandReset.java b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandReset.java index be709303..74db1bae 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandReset.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandReset.java @@ -1,28 +1,27 @@ package net.famzangl.minecraft.minebot.build.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; -import net.famzangl.minecraft.minebot.ai.task.AITask; -import net.famzangl.minecraft.minebot.build.NextTaskTask; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; @AICommand(helpText = "Reset the internal build queue.", name = "minebuild") public class CommandReset { - private static final class ResetStrategy implements AIStrategy { + private static final class ResetStrategy extends AIStrategy { @Override - public void searchTasks(AIHelper helper) { - if (helper.buildManager.peekNextTask() != null) { - helper.addTask(new NextTaskTask(10000)); - } + public boolean checkShouldTakeOver(AIHelper helper) { + return helper.buildManager.peekNextTask() != null; } @Override - public AITask getOverrideTask(AIHelper helper) { - return null; + protected TickResult onGameTick(AIHelper helper) { + while (helper.buildManager.peekNextTask() != null) { + helper.buildManager.popNextTask(); + } + return TickResult.NO_MORE_WORK; } @Override diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandReverse.java b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandReverse.java index 87921b16..2b772b02 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandReverse.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandReverse.java @@ -6,11 +6,11 @@ import java.util.Calendar; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.build.reverse.RunReverseBuildStrategy; import net.minecraft.client.Minecraft; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandScheduleBuild.java b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandScheduleBuild.java index 75c9ad68..55ecc18d 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandScheduleBuild.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandScheduleBuild.java @@ -2,12 +2,12 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.CommandEvaluationException; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.task.BlockSide; import net.famzangl.minecraft.minebot.build.WoodType; import net.famzangl.minecraft.minebot.build.blockbuild.BlockBuildTask; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandSetPos.java b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandSetPos.java index cb578b85..22db21d5 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandSetPos.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandSetPos.java @@ -2,11 +2,11 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; @AICommand(helpText = "Set bounding position manually.", name = "minebuild") public class CommandSetPos { @@ -26,7 +26,7 @@ public static AIStrategy run( helper.setPosition(helper.getPlayerPosition(), true); return null; } - + @AICommandInvocation() public static AIStrategy run( AIHelper helper, diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandStepNext.java b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandStepNext.java index 6ae81c06..e48c1480 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandStepNext.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandStepNext.java @@ -1,13 +1,12 @@ package net.famzangl.minecraft.minebot.build.commands; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; -import net.famzangl.minecraft.minebot.ai.task.AITask; -import net.famzangl.minecraft.minebot.build.NextTaskTask; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.RunOnceStrategy; @AICommand(helpText = "Select the next task as active.", name = "minebuild") public class CommandStepNext { @@ -17,26 +16,13 @@ public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "step", description = "") String nameArg2, @AICommandParameter(type = ParameterType.FIXED, fixedName = "next", description = "") String nameArg3) { - return new AIStrategy() { - private boolean done; - + return new RunOnceStrategy() { @Override - public void searchTasks(AIHelper helper) { - if (!done) { - helper.addTask(new NextTaskTask()); - done = true; + protected void singleRun(AIHelper helper) { + if (helper.buildManager.peekNextTask() != null) { + helper.buildManager.popNextTask(); } } - - @Override - public AITask getOverrideTask(AIHelper helper) { - return null; - } - - @Override - public String getDescription() { - return null; - } }; } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandStepPlace.java b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandStepPlace.java index ebc61967..c160e00d 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandStepPlace.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandStepPlace.java @@ -2,12 +2,14 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AIChatController; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.TaskStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; import net.famzangl.minecraft.minebot.ai.task.AITask; import net.famzangl.minecraft.minebot.build.blockbuild.BuildTask; @@ -19,9 +21,10 @@ public static AIStrategy run( AIHelper helper, @AICommandParameter(type = ParameterType.FIXED, fixedName = "step", description = "") String nameArg2, @AICommandParameter(type = ParameterType.FIXED, fixedName = "place", description = "") String nameArg3) { - return new AIStrategy() { + return ValueActionStrategy.makeSafe(new TaskStrategy() { + @Override - public void searchTasks(AIHelper helper) { + protected void searchTasks(AIHelper helper) { final BuildTask task = helper.buildManager.peekNextTask(); if (task == null) { AIChatController.addChatLine("No more build tasks."); @@ -30,33 +33,28 @@ public void searchTasks(AIHelper helper) { } } - @Override - public AITask getOverrideTask(AIHelper helper) { - return null; + private void addStep(AIHelper helper, final BuildTask task) { + final Pos forPosition = task.getForPosition(); + final Pos fromPos = getFromPos(helper, task, forPosition); + if (fromPos == null) { + AIChatController.addChatLine("Not at starting position."); + } else if (helper.isAirBlock(task.getForPosition().x, + task.getForPosition().y, task.getForPosition().z)) { + final AITask t = task.getPlaceBlockTask(fromPos); + if (t != null) { + addTask(t); + } + } else { + AIChatController + .addChatLine("Could not place the block: There is already something!"); + } } @Override public String getDescription() { return "Place the block."; } - }; - } - - public static void addStep(AIHelper helper, final BuildTask task) { - final Pos forPosition = task.getForPosition(); - final Pos fromPos = getFromPos(helper, task, forPosition); - if (fromPos == null) { - AIChatController.addChatLine("Not at starting position."); - } else if (helper.isAirBlock(task.getForPosition().x, - task.getForPosition().y, task.getForPosition().z)) { - final AITask t = task.getPlaceBlockTask(fromPos); - if (t != null) { - helper.addTask(t); - } - } else { - AIChatController - .addChatLine("Could not place the block: There is already something!"); - } + }); } private static Pos getFromPos(AIHelper helper, final BuildTask task, diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandStepWalk.java b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandStepWalk.java index 0e405143..7dbd172d 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandStepWalk.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/commands/CommandStepWalk.java @@ -2,13 +2,14 @@ import net.famzangl.minecraft.minebot.Pos; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; import net.famzangl.minecraft.minebot.ai.command.AIChatController; import net.famzangl.minecraft.minebot.ai.command.AICommand; import net.famzangl.minecraft.minebot.ai.command.AICommandInvocation; import net.famzangl.minecraft.minebot.ai.command.AICommandParameter; import net.famzangl.minecraft.minebot.ai.command.ParameterType; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; import net.famzangl.minecraft.minebot.ai.strategy.PathFinderStrategy; +import net.famzangl.minecraft.minebot.ai.strategy.ValueActionStrategy; import net.famzangl.minecraft.minebot.build.ForBuildPathFinder; import net.famzangl.minecraft.minebot.build.blockbuild.BuildTask; @@ -26,8 +27,9 @@ public static AIStrategy run( AIChatController.addChatLine("No more build tasks."); return null; } else { - final ForBuildPathFinder pf = new ForBuildPathFinder(helper, task); - return new PathFinderStrategy(pf, "Going to building site.") { + final ForBuildPathFinder pf = new ForBuildPathFinder(task); + return ValueActionStrategy.makeSafe(new PathFinderStrategy(pf, + "Going to building site.") { @Override public void searchTasks(AIHelper helper) { final Pos atTarget = CommandBuild @@ -36,7 +38,7 @@ public void searchTasks(AIHelper helper) { super.searchTasks(helper); } } - }; + }); } } } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/reverse/BuildReverser.java b/Minebot/src/net/famzangl/minecraft/minebot/build/reverse/BuildReverser.java index d5e1c451..6327c4ad 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/reverse/BuildReverser.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/reverse/BuildReverser.java @@ -65,8 +65,9 @@ public void run() { } out.println("#/minebuild build"); - if (missingBlocks >0) { - AIChatController.addChatLine("Could not convert " + missingBlocks + "blocks. They will be missing."); + if (missingBlocks > 0) { + AIChatController.addChatLine("Could not convert " + + missingBlocks + "blocks. They will be missing."); } AIChatController.addChatLine("Output written to: " + this.outFile); } catch (final FileNotFoundException e) { diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/reverse/RunReverseBuildStrategy.java b/Minebot/src/net/famzangl/minecraft/minebot/build/reverse/RunReverseBuildStrategy.java index d9892bdc..b76756af 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/reverse/RunReverseBuildStrategy.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/reverse/RunReverseBuildStrategy.java @@ -1,10 +1,10 @@ package net.famzangl.minecraft.minebot.build.reverse; import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.AIStrategy; -import net.famzangl.minecraft.minebot.ai.task.AITask; +import net.famzangl.minecraft.minebot.ai.command.AIChatController; +import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy; -public final class RunReverseBuildStrategy implements AIStrategy { +public final class RunReverseBuildStrategy extends AIStrategy { final String outFile; private boolean done = false; @@ -13,16 +13,21 @@ public RunReverseBuildStrategy(String outFile) { } @Override - public void searchTasks(AIHelper helper) { - if (!done) { - helper.addTask(new RunReverseBuildTask(outFile)); - done = true; - } + public boolean checkShouldTakeOver(AIHelper helper) { + return !done; } @Override - public AITask getOverrideTask(AIHelper helper) { - return null; + protected TickResult onGameTick(AIHelper helper) { + if (!done) { + if (helper.getPos1() == null || helper.getPos2() == null) { + AIChatController.addChatLine("Set positions first."); + } else { + new BuildReverser(helper, this.outFile).run(); + } + done = true; + } + return TickResult.NO_MORE_WORK; } @Override diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/reverse/RunReverseBuildTask.java b/Minebot/src/net/famzangl/minecraft/minebot/build/reverse/RunReverseBuildTask.java deleted file mode 100644 index dcc77d3b..00000000 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/reverse/RunReverseBuildTask.java +++ /dev/null @@ -1,30 +0,0 @@ -package net.famzangl.minecraft.minebot.build.reverse; - -import net.famzangl.minecraft.minebot.ai.AIHelper; -import net.famzangl.minecraft.minebot.ai.command.AIChatController; -import net.famzangl.minecraft.minebot.ai.task.AITask; - -final class RunReverseBuildTask extends AITask { - final String outFile; - - RunReverseBuildTask(String outFile) { - this.outFile = outFile; - } - - private boolean done = false; - - @Override - public void runTick(AIHelper h) { - if (h.getPos1() == null || h.getPos2() == null) { - AIChatController.addChatLine("Set positions first."); - } else { - new BuildReverser(h, this.outFile).run(); - } - done = true; - } - - @Override - public boolean isFinished(AIHelper h) { - return done; - } -} \ No newline at end of file