Support displaying caught items

This commit is contained in:
BruceChen 2022-10-17 17:42:00 +08:00
parent b949db57cf
commit bce2bc8b7f
6 changed files with 83 additions and 7 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using MinecraftClient.Inventory;
using MinecraftClient.Mapping;
using Tomlet.Attributes;
@ -142,6 +143,8 @@ namespace MinecraftClient.ChatBots
private Entity? fishingBobber;
private Location LastPos = Location.Zero;
private DateTime CaughtTime = DateTime.Now;
private int fishItemCounter = 10;
private Entity fish = new(-1, EntityType.Item, Location.Zero);
private int counter = 0;
private readonly object stateLock = new();
@ -243,6 +246,7 @@ namespace MinecraftClient.ChatBots
isFishing = false;
state = FishingState.Stopping;
}
fishItemCounter = 10;
}
private void UseFishRod()
@ -255,6 +259,8 @@ namespace MinecraftClient.ChatBots
public override void Update()
{
if (fishItemCounter < 10)
++fishItemCounter;
lock (stateLock)
{
switch (state)
@ -341,11 +347,20 @@ namespace MinecraftClient.ChatBots
public override void OnEntitySpawn(Entity entity)
{
if (entity.Type == EntityType.FishingBobber && entity.ObjectData == GetPlayerEntityID())
if (fishItemCounter < 10 && entity.Type == EntityType.Item && Math.Abs(entity.Location.Y - LastPos.Y) < 2.0 &&
Math.Abs(entity.Location.X - LastPos.X) < 0.1 && Math.Abs(entity.Location.Z - LastPos.Z) < 0.1)
{
if (Config.Log_Fish_Bobber)
LogToConsole(string.Format("Item ({0}) spawn at {1}, distance = {2:0.00}", entity.ID, entity.Location, entity.Location.Distance(LastPos)));
fish = entity;
}
else if (entity.Type == EntityType.FishingBobber && entity.ObjectData == GetPlayerEntityID())
{
if (Config.Log_Fish_Bobber)
LogToConsole(string.Format("FishingBobber spawn at {0}, distance = {1:0.00}", entity.Location, GetCurrentLocation().Distance(entity.Location)));
fishItemCounter = 10;
LogToConsole(GetTimestamp() + ": " + Translations.Get("bot.autoFish.throw"));
lock (stateLock)
{
@ -387,7 +402,8 @@ namespace MinecraftClient.ChatBots
public override void OnEntityMove(Entity entity)
{
if (isFishing && entity != null && fishingBobber!.ID == entity.ID)
if (isFishing && entity != null && fishingBobber!.ID == entity.ID &&
(state == FishingState.WaitingFishToBite || state == FishingState.WaitingFishingBobber))
{
Location Pos = entity.Location;
double Dx = LastPos.X - Pos.X;
@ -413,6 +429,15 @@ namespace MinecraftClient.ChatBots
}
}
public override void OnEntityMetadata(Entity entity, Dictionary<int, object?> metadata)
{
if (fishItemCounter < 10 && entity.ID == fish.ID && metadata.TryGetValue(8, out object? item))
{
LogToConsole(Translations.Get("bot.autoFish.got", ((Item)item!).ToFullString()));
fishItemCounter = 10;
}
}
public override void AfterGameJoined()
{
StartFishing();
@ -453,10 +478,11 @@ namespace MinecraftClient.ChatBots
lock (stateLock)
{
UseFishRod();
counter = 0;
state = FishingState.StartMove;
fishItemCounter = 0;
UseFishRod();
}
}