Complete villager trading job

This commit is contained in:
Tanner Collin 2021-02-24 08:21:25 +00:00
parent 28ca442a46
commit fcc74c7198
2 changed files with 57 additions and 47 deletions

View File

@ -107,8 +107,9 @@ CARROT_ID = get_id('carrot')
POTATO_ID = get_id('potato')
WHEAT_SEEDS_ID = get_id('wheat_seeds')
BEETROOT_SEEDS_ID = get_id('beetroot_seeds')
PUMPKIN_ID = get_id('pumpkin')
EMERALD_ID = get_id('emerald')
NEEDED_ITEMS = BED_IDS | SHOVEL_IDS | AXE_IDS | FOOD_IDS | set([CHEST_ID])
NEEDED_ITEMS = BED_IDS | SHOVEL_IDS | AXE_IDS | FOOD_IDS | set([CHEST_ID, PUMPKIN_ID])
WANTED_ITEMS = SAPLING_IDS | set([NETHERWART_ID, CARROT_ID, POTATO_ID, WHEAT_SEEDS_ID, BEETROOT_SEEDS_ID])

95
jobs.py
View File

@ -3,7 +3,7 @@ import time
import importlib
import random
from itertools import count
from math import hypot
from math import hypot, floor
from minecraft.networking.types import BlockFace
@ -1049,12 +1049,13 @@ class GrabSuppliesStates:
self.state = self.cleanup
def check_supplies(self):
for items, maximum in self.supplies.items():
for items, limits in self.supplies.items():
minimum, maximum = limits
print('Checking items:', items)
num_items = self.g.game.count_items(items)
print('Have:', num_items)
if num_items:
if num_items >= minimum:
print('Skipping')
continue
else:
@ -1843,12 +1844,21 @@ class SellToVillagerStates:
print('Interacting with villager')
self.g.game.interact(self.villager.entity_id)
self.g.game.animate()
self.wait_time = 0.5
self.state = self.wait_for_window
def wait_for_window(self):
if self.wait_time > 0:
self.wait_time -= utils.TICK
else:
self.state = self.choose_trade
def choose_trade(self):
g = self.g.game
if not self.g.window or not self.g.trades:
print('Didnt get a trade window, aborting')
self.state = self.cleanup
return
for trade_num, trade in enumerate(self.g.trades):
@ -1856,26 +1866,40 @@ class SellToVillagerStates:
in_num = trade.input_item_1.item_count
out_id = trade.output_item.item_id
price = in_num \
+ floor(in_num * trade.price_multiplier * trade.demand) \
+ trade.special_price
if g.count_items([in_id]) < in_num:
if g.count_items([in_id]) < price:
continue
print('Checking trade #', trade_num)
if trade.trade_disabled:
print('Trade disabled, skipping')
continue
if out_id != items.EMERALD_ID:
print('Not for emeralds, skipping')
continue
if price > in_num:
print('Trade in demand, skipping')
continue
print('Found trade: #', trade_num, trade)
print('Found trade:', trade)
print('Adjusted price:', price)
self.trade = trade
self.trade_num = trade_num
self.state = self.click_trade
break
else:
print('Villager has been spent, aborting')
self.g.game.close_window()
self.spent_villagers.append(self.villager)
self.state = self.cleanup
self.state = self.wait
self.wait_time = 10
return
def click_trade(self):
print('Clicking trade')
self.g.item_lock = True
self.g.game.select_trade(self.trade_num)
self.state = self.execute_trade
@ -1887,43 +1911,28 @@ class SellToVillagerStates:
w = self.g.window
w_info = mcdata.WINDOWS[w.data.window_type]
slot_num = w_info.output
slot = w.contents[slot_num]
self.g.item_lock = True
if slot_num in w.contents:
print('Executing trade')
slot = w.contents[slot_num]
self.g.game.click_window(slot_num, 0, 1, slot)
self.g.game.close_window()
else:
print('Bad trade, aborting')
self.state = self.end_trade
def end_trade(self):
if self.g.item_lock:
return
print('Trading complete')
self.g.game.close_window()
self.wait_time = 1
self.state = self.wait_for_trade
print('Trade completed.')
def wait_for_trade(self):
if self.wait_time > 0:
self.wait_time -= utils.TICK
else:
self.state = self.interact_villager
#def grab_stack(self):
# g = self.g.game
# in_id = self.trade.input_item_1.item_id
# slot_num, slot = g.get_window_slot(in_id)
# if not slot:
# print('Item not found, aborting')
# self.state = self.cleanup
# return
# print('Grabbing:', slot)
# self.g.item_lock = True
# # double click stack to grab max
# self.g.game.click_window(slot_num, 0, 6, slot)
# self.state = self.freeze
def freeze(self):
return
def wait(self):
if self.wait_time > 0:
self.wait_time -= utils.TICK
@ -2010,7 +2019,7 @@ class JobStates:
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
self.grab_supplies_states.supplies = {
tuple(items.SHOVEL_IDS): 9
tuple(items.SHOVEL_IDS): (1, 9),
}
return machines
@ -2050,7 +2059,7 @@ class JobStates:
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
self.grab_supplies_states.supplies = {
tuple(items.AXE_IDS): 9,
tuple(items.AXE_IDS): (1, 9),
}
return machines
@ -2091,7 +2100,7 @@ class JobStates:
item = items.ITEMS['minecraft:'+name]['protocol_id']
self.grab_supplies_states.supplies = {
tuple([item]): 0,
tuple([item]): (1, 0),
}
return machines
@ -2106,16 +2115,16 @@ class JobStates:
def trade(self):
machines = [
#self.grab_supplies_states,
self.grab_supplies_states,
self.sell_to_villager,
#self.sleep_with_bed_states,
#self.eat_food_states,
#self.cache_items_states,
self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states,
]
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
self.grab_supplies_states.supplies = {
tuple(items.AXE_IDS): 9,
tuple([items.PUMPKIN_ID]): (64, 9),
}
return machines