diff --git a/src/main/java/baritone/behavior/impl/MemoryBehavior.java b/src/main/java/baritone/behavior/impl/MemoryBehavior.java index ed5bd6860..f8009c436 100644 --- a/src/main/java/baritone/behavior/impl/MemoryBehavior.java +++ b/src/main/java/baritone/behavior/impl/MemoryBehavior.java @@ -1,9 +1,14 @@ package baritone.behavior.impl; import baritone.behavior.Behavior; +import baritone.chunk.Waypoint; +import baritone.chunk.WorldProvider; +import baritone.event.events.BlockInteractEvent; import baritone.event.events.PacketEvent; import baritone.event.events.PlayerUpdateEvent; import baritone.event.events.type.EventState; +import baritone.utils.BlockStateInterface; +import net.minecraft.block.BlockBed; import net.minecraft.item.ItemStack; import net.minecraft.network.Packet; import net.minecraft.network.play.client.CPacketCloseWindow; @@ -104,6 +109,13 @@ public class MemoryBehavior extends Behavior { } } + @Override + public void onBlockInteract(BlockInteractEvent event) { + if (event.getType() == BlockInteractEvent.Type.USE && BlockStateInterface.get(event.getPos()) instanceof BlockBed) { + WorldProvider.INSTANCE.getCurrentWorld().waypoints.addWaypoint(new Waypoint("bed", Waypoint.Tag.BED, playerFeet())); + } + } + private Optional getInventoryFromWindow(int windowId) { return this.rememberedInventories.values().stream().filter(i -> i.windowId == windowId).findFirst(); } diff --git a/src/main/java/baritone/chunk/Waypoints.java b/src/main/java/baritone/chunk/Waypoints.java index 255ac03d4..2ad7f16d3 100644 --- a/src/main/java/baritone/chunk/Waypoints.java +++ b/src/main/java/baritone/chunk/Waypoints.java @@ -61,7 +61,7 @@ public class Waypoints { try ( FileInputStream fileIn = new FileInputStream(fileName.toFile()); BufferedInputStream bufIn = new BufferedInputStream(fileIn); - DataInputStream in = new DataInputStream(bufIn); + DataInputStream in = new DataInputStream(bufIn) ) { while (true) { String name = in.readUTF(); @@ -71,7 +71,7 @@ public class Waypoints { int z = in.readInt(); waypoints.get(tag).add(new Waypoint(name, tag, new BlockPos(x, y, z), creationTimestamp)); } - } catch (IOException ex) { } + } catch (IOException ignored) {} } private synchronized void save(Waypoint.Tag tag) { diff --git a/src/main/java/baritone/event/GameEventHandler.java b/src/main/java/baritone/event/GameEventHandler.java index c6ab20fdd..c61201bf3 100644 --- a/src/main/java/baritone/event/GameEventHandler.java +++ b/src/main/java/baritone/event/GameEventHandler.java @@ -174,6 +174,11 @@ public final class GameEventHandler implements IGameEventListener, Helper { dispatch(listener -> listener.onPlayerRelativeMove(event)); } + @Override + public void onBlockInteract(BlockInteractEvent event) { + dispatch(listener -> listener.onBlockInteract(event)); + } + public final void registerEventListener(IGameEventListener listener) { this.listeners.add(listener); } diff --git a/src/main/java/baritone/event/events/BlockInteractEvent.java b/src/main/java/baritone/event/events/BlockInteractEvent.java new file mode 100644 index 000000000..c458f6b61 --- /dev/null +++ b/src/main/java/baritone/event/events/BlockInteractEvent.java @@ -0,0 +1,69 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Baritone. If not, see . + */ + +package baritone.event.events; + +import net.minecraft.util.math.BlockPos; + +/** + * @author Brady + * @since 8/22/2018 + */ +public final class BlockInteractEvent { + + /** + * The position of the block interacted with + */ + private final BlockPos pos; + + /** + * The type of interaction that occurred + */ + private final Type type; + + public BlockInteractEvent(BlockPos pos, Type type) { + this.pos = pos; + this.type = type; + } + + /** + * @return The position of the block interacted with + */ + public final BlockPos getPos() { + return this.pos; + } + + /** + * @return The type of interaction with the target block + */ + public final Type getType() { + return this.type; + } + + public enum Type { + + /** + * We're breaking the target block. + */ + BREAK, + + /** + * We're right clicking on the target block. Either placing or interacting with. + */ + USE + } +} diff --git a/src/main/java/baritone/event/listener/AbstractGameEventListener.java b/src/main/java/baritone/event/listener/AbstractGameEventListener.java index 46cbeb650..4f940c388 100644 --- a/src/main/java/baritone/event/listener/AbstractGameEventListener.java +++ b/src/main/java/baritone/event/listener/AbstractGameEventListener.java @@ -80,4 +80,7 @@ public interface AbstractGameEventListener extends IGameEventListener { @Override default void onPlayerRelativeMove(RelativeMoveEvent event) {} + + @Override + default void onBlockInteract(BlockInteractEvent event) {} } diff --git a/src/main/java/baritone/event/listener/IGameEventListener.java b/src/main/java/baritone/event/listener/IGameEventListener.java index 7d2391339..1958f0c3a 100644 --- a/src/main/java/baritone/event/listener/IGameEventListener.java +++ b/src/main/java/baritone/event/listener/IGameEventListener.java @@ -132,4 +132,12 @@ public interface IGameEventListener { * @see Entity#moveRelative(float, float, float, float) */ void onPlayerRelativeMove(RelativeMoveEvent event); + + /** + * Called when the local player interacts with a block, whether it is breaking or opening/placing. + * + * @see Minecraft#clickMouse() + * @see Minecraft#rightClickMouse() + */ + void onBlockInteract(BlockInteractEvent event); } diff --git a/src/main/java/baritone/launch/mixins/MixinMinecraft.java b/src/main/java/baritone/launch/mixins/MixinMinecraft.java index 1c14d1792..487f126fc 100755 --- a/src/main/java/baritone/launch/mixins/MixinMinecraft.java +++ b/src/main/java/baritone/launch/mixins/MixinMinecraft.java @@ -19,6 +19,7 @@ package baritone.launch.mixins; import baritone.Baritone; import baritone.behavior.impl.PathingBehavior; +import baritone.event.events.BlockInteractEvent; import baritone.event.events.TickEvent; import baritone.event.events.WorldEvent; import baritone.event.events.type.EventState; @@ -26,6 +27,10 @@ import baritone.utils.ExampleBaritoneControl; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.multiplayer.WorldClient; +import net.minecraft.item.ItemStack; +import net.minecraft.util.EnumActionResult; +import net.minecraft.util.EnumHand; +import net.minecraft.util.math.BlockPos; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -33,6 +38,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; /** * @author Brady @@ -155,4 +161,27 @@ public class MixinMinecraft { private boolean isAllowUserInput(GuiScreen screen) { return PathingBehavior.INSTANCE.getCurrent() != null || screen.allowUserInput; } + + @Inject( + method = "clickMouse", + at = @At( + value = "INVOKE", + target = "net/minecraft/client/multiplayer/PlayerControllerMP.clickBlock(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/EnumFacing;)Z" + ) + ) + private void clickMouse(CallbackInfo ci, BlockPos pos) { + Baritone.INSTANCE.getGameEventHandler().onBlockInteract(new BlockInteractEvent(pos, BlockInteractEvent.Type.BREAK)); + } + + @Inject( + method = "rightClickMouse", + at = @At( + value = "INVOKE", + target = "net/minecraft/client/entity/EntityPlayerSP.swingArm(Lnet/minecraft/util/EnumHand;)V" + ), + locals = LocalCapture.CAPTURE_FAILHARD + ) + private void onBlockInteract(CallbackInfo ci, EnumHand var1[], int var2, int var3, EnumHand enumhand, ItemStack itemstack, BlockPos blockpos, int i, EnumActionResult enumactionresult) { + Baritone.INSTANCE.getGameEventHandler().onBlockInteract(new BlockInteractEvent(blockpos, BlockInteractEvent.Type.USE)); + } }