Add automatic bed waypoints on interact, fixes #79

This commit is contained in:
Brady 2018-08-22 21:04:37 -05:00
parent fcb947bedc
commit 060eeb9737
No known key found for this signature in database
GPG Key ID: 73A788379A197567
7 changed files with 128 additions and 2 deletions

View File

@ -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<RememberedInventory> getInventoryFromWindow(int windowId) {
return this.rememberedInventories.values().stream().filter(i -> i.windowId == windowId).findFirst();
}

View File

@ -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) {

View File

@ -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);
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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
}
}

View File

@ -80,4 +80,7 @@ public interface AbstractGameEventListener extends IGameEventListener {
@Override
default void onPlayerRelativeMove(RelativeMoveEvent event) {}
@Override
default void onBlockInteract(BlockInteractEvent event) {}
}

View File

@ -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);
}

View File

@ -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));
}
}