Add state machine for caching items into chests

This commit is contained in:
2020-09-20 19:08:23 -06:00
parent b8cbd9a5f0
commit f8d44e7e38
12 changed files with 326 additions and 59 deletions
+61 -7
View File
@@ -3,13 +3,14 @@ import time
import importlib
from math import hypot
from itertools import count
from bunch import Bunch
from panda3d.core import LPoint3f
from minecraft.networking.packets import Packet, clientbound, serverbound
from minecraft.networking.types import BlockFace
from protocol.packets import TimeUpdatePacket, SetSlotPacket, PlayerDiggingPacket, BlockBreakAnimationPacket, AcknowledgePlayerDiggingPacket, HeldItemChangePacket, PickItemPacket
from protocol.packets import TimeUpdatePacket, SetSlotPacket, PlayerDiggingPacket, BlockBreakAnimationPacket, AcknowledgePlayerDiggingPacket, HeldItemChangePacket, PickItemPacket, OpenWindowPacket, ClickWindowPacket, CloseWindowPacket
import utils
importlib.reload(utils)
@@ -19,6 +20,8 @@ import blocks
importlib.reload(blocks)
import items
importlib.reload(items)
import data
importlib.reload(data)
class MCWorld:
def __init__(self, global_state):
@@ -122,6 +125,9 @@ class MCWorld:
areas.sort(key=lambda x: utils.phyp(center, x))
return areas
def find_cache_areas(self, center, distance):
return self.find_bed_areas(center, distance)
def sand_adjacent_safe(self, sand):
for direction in path.CHECK_DIRECTIONS:
if self.block_at(*utils.padd(sand, direction)) in blocks.AVOID_IDS:
@@ -163,6 +169,9 @@ class MCWorld:
result.append(utils.padd(area, direction))
return result
def find_cache_openings(self, area):
return self.find_bed_openings(area)
class Game:
def __init__(self, global_state):
@@ -176,6 +185,7 @@ class Game:
register(self.handle_set_slot, SetSlotPacket)
register(self.handle_break_animation, BlockBreakAnimationPacket)
register(self.handle_break_ack, AcknowledgePlayerDiggingPacket)
register(self.handle_window, OpenWindowPacket)
self.g.chat.set_handler(self.handle_chat)
@@ -314,7 +324,22 @@ class Game:
reply = 'not found'
if command == 'open':
self.place_block(blocks.TEST_BLOCK, BlockFace.TOP)
self.open_container(blocks.TEST_BLOCK)
if command == 'close':
if self.g.window:
self.close_window()
else:
reply = 'nothing open'
if command == 'click' and data:
if self.g.window:
window_id, slot, button, mode = [int(x) for x in data.split(' ')]
item = self.g.window.contents[slot]
print(item)
self.click_window(window_id, slot, button, mode, item)
else:
reply = 'nothing open'
if reply:
print(reply)
@@ -324,12 +349,15 @@ class Game:
self.g.time = packet.time_of_day % 24000
def handle_set_slot(self, packet):
g = self.g
print(packet)
if packet.window_id == 0:
self.g.inv[packet.slot] = packet.slot_data
g.inv[packet.slot] = packet.slot_data
elif g.window:
g.window.contents[packet.slot] = packet.slot_data
if not packet.slot_data.present:
self.g.dump_lock = False
if not packet.slot_data.present:
g.item_lock = False
def break_block(self, location):
bid = self.g.chunks.get_block_at(*location)
@@ -416,6 +444,32 @@ class Game:
packet.face = 1
self.g.connection.write_packet(packet)
def open_container(self, location):
bid = self.g.chunks.get_block_at(*location)
# TODO: check if block is a chest??
self.place_block(location, BlockFace.TOP)
def handle_window(self, packet):
print(packet)
self.g.window = Bunch(data=packet, contents=dict())
def click_window(self, window_id, slot, button, mode, item):
packet = ClickWindowPacket()
packet.window_id = window_id
packet.slot = slot
packet.button = button
packet.action_number = 1
packet.mode = mode
packet.clicked_item = item
self.g.connection.write_packet(packet)
def close_window(self):
packet = CloseWindowPacket()
packet.window_id = self.g.window.data.window_id
self.g.connection.write_packet(packet)
self.g.window = None
def tick(self):
if self.g.breaking:
self.animate()
@@ -423,10 +477,10 @@ class Game:
if time.time() >= self.g.break_time - 2*utils.TICK:
self.break_finish()
if self.g.dumping and not self.g.dump_lock:
if self.g.dumping and not self.g.item_lock:
if self.select_item([self.g.dumping]):
self.drop_stack()
self.g.dump_lock = True
self.g.item_lock = True
else:
self.g.dumping = None