From d108d7c81739b5b145e20459f5468d89581a08db Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Thu, 17 Dec 2020 00:02:56 +0000 Subject: [PATCH] Add a job for grabbing supplies from barrels --- blocks.py | 6 +++ items.py | 13 +++++ jobs.py | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) diff --git a/blocks.py b/blocks.py index 39183bc..b413a92 100644 --- a/blocks.py +++ b/blocks.py @@ -241,6 +241,7 @@ INDEXED = [ 'chest', 'trapped_chest', 'emerald_block', + 'barrel', ] @@ -274,6 +275,11 @@ for block_name in ['chest']: for state in JSON_BLOCKS['minecraft:' + block_name]['states']: CHEST_IDS.add(state['id']) +BARREL_IDS = set() +for block_name in ['barrel']: + for state in JSON_BLOCKS['minecraft:' + block_name]['states']: + BARREL_IDS.add(state['id']) + TRAPPED_CHEST_IDS = set() for block_name in ['trapped_chest']: for state in JSON_BLOCKS['minecraft:' + block_name]['states']: diff --git a/items.py b/items.py index 89f58e0..786896f 100644 --- a/items.py +++ b/items.py @@ -31,6 +31,15 @@ SHOVELS = [ 'netherite_shovel', ] +AXES = [ + 'wooden_axe', + 'stone_axe', + 'golden_axe', + 'iron_axe', + 'diamond_axe', + 'netherite_axe', +] + SAPLINGS = [ 'oak_sapling', 'spruce_sapling', @@ -48,6 +57,10 @@ SHOVEL_IDS = set() for item_name in SHOVELS: SHOVEL_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id']) +AXE_IDS = set() +for item_name in AXES: + AXE_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id']) + SAPLING_IDS = set() for item_name in SAPLINGS: SAPLING_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id']) diff --git a/jobs.py b/jobs.py index f1fb3b4..3d450ca 100644 --- a/jobs.py +++ b/jobs.py @@ -1034,6 +1034,158 @@ class CacheItemsStates: self.state() + +class GrabSuppliesStates: + def idle(self): + return None + + def init(self): + if self.supplies: + self.state = self.check_supplies + else: + print('Aborting getting supplies, none specified') + self.state = self.cleanup + + def check_supplies(self): + for items, maximum in self.supplies.items(): + print('Checking items:', items) + num_items = self.g.game.count_items(items) + print('Have:', num_items) + + if num_items: + print('Skipping') + continue + else: + self.target_items = items + self.maximum_items = maximum + self.count = 0 + self.state = self.find_barrels + return + + print('Aborting, don\'t need any more supplies') + self.state = self.cleanup + + def find_barrels(self): + print('Finding barrels...') + w = self.g.world + p = utils.pint(self.g.pos) + + self.barrels = w.find_blocks_indexed(p, blocks.BARREL_IDS, 100) + print('Found:', self.barrels) + self.state = self.choose_barrel + + def choose_barrel(self): + print('Choosing a barrel...') + w = self.g.world + p = utils.pint(self.g.pos) + c = self.g.chunks + + if not len(self.barrels): + print('No barrels') + self.state = self.cleanup + return + + barrel = self.barrels[0] + + tmp = c.get_block_at(*barrel) + c.set_block_at(*barrel, blocks.AIR) + navpath = w.path_to_place(p, barrel) + c.set_block_at(*barrel, tmp) + + print('navpath:', navpath) + + if navpath: + self.g.path = navpath[:-1] + self.opening = self.g.path[-1] + self.area = barrel + self.state = self.going_to_barrel + return + else: + self.barrel.pop(0) + + def going_to_barrel(self): + if utils.pint(self.g.pos) == self.opening: + self.g.look_at = self.area + self.state = self.open_barrel + + def open_barrel(self): + print('Opening barrel') + self.g.game.open_container(self.area) + self.wait_time = 1 + self.state = self.wait + + def wait(self): + # wait for server to send us barrel contents + if self.wait_time > 0: + self.wait_time -= utils.TICK + else: + self.state = self.grab_items + + def grab_items(self): + if self.g.item_lock: return + + w = self.g.window + w_info = mcdata.WINDOWS[w.data.window_type] + w_container_slots = w_info.container + + for slot_num in w_container_slots: + if slot_num not in w.contents: + continue + + slot = w.contents[slot_num] + + if not slot.present: + continue + + if slot.item_id not in self.target_items: + continue + + if self.count >= self.maximum_items: + break + self.count += 1 + + print('Moving', slot) + + self.g.item_lock = True + self.g.game.click_window(slot_num, 0, 1, slot) + return + + print('Nothing left to move') + self.state = self.close_barrel + + def close_barrel(self): + print('Closing barrel') + self.g.game.close_window() + self.state = self.cleanup + + def cleanup(self): + self.g.look_at = None + self.state = self.done + + def done(self): + # never gets ran, placeholder + return None + + def __init__(self, global_state): + self.g = global_state + self.state = self.idle + + self.supplies = {} + self.target_supplies = [] + self.barrels = [] + + self.target_items = None + self.maximum_items = 0 + self.count = 0 + + self.area = None + self.opening = None + self.wait_time = 0 + + def run(self): + self.state() + + class PlantTreeStates: def idle(self): return None @@ -1558,6 +1710,7 @@ class JobStates: self.gather_sand_states = GatherSandStates(self.g) self.sleep_with_bed_states = SleepWithBedStates(self.g) self.cache_items_states = CacheItemsStates(self.g) + self.grab_supplies_states = GrabSuppliesStates(self.g) self.find_gapple_states = FindGappleStates(self.g) self.plant_tree_states = PlantTreeStates(self.g) self.clear_leaves_states = ClearLeavesStates(self.g) @@ -1623,6 +1776,7 @@ class JobStates: def farm_wood(self): machines = [ + self.grab_supplies_states, self.gather_wood_states, self.clear_leaves_states, self.plant_tree_states, @@ -1632,6 +1786,9 @@ 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, + } return machines def farm_wart(self):