Created
July 7, 2012 20:36
-
-
Save dequis/3068013 to your computer and use it in GitHub Desktop.
Freecam plugin for mc3p
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mc3p.plugins import MC3Plugin, msghdlr | |
class FreecamPlugin(MC3Plugin): | |
def init(self, args): | |
self.freecam = False | |
self.last_pos = None | |
self.safe = True | |
self.abilities = None | |
def send_chat(self, chat_msg): | |
self.to_client({'msgtype': 0x03, 'chat_msg': chat_msg}) | |
def is_safe(self, current_pos, threshold=4): | |
get_coords = lambda m: (m['x'], m['y'], m['z']) | |
result = map(lambda x, y: abs(x - y) < threshold, | |
get_coords(current_pos), get_coords(self.last_pos)) | |
return not (False in result) | |
@msghdlr(0x0a, 0x0b, 0x0c, 0x0d) | |
def handle_position(self, msg, source): | |
if source == 'client': | |
if self.freecam: | |
if msg['msgtype'] in (0x0b, 0x0d) and self.last_pos: | |
self.safe = self.is_safe(msg) | |
return False | |
elif msg['msgtype'] == 0x0d: | |
self.last_pos = msg | |
return True | |
@msghdlr(0xca) | |
def handle_abilities(self, msg, source): | |
self.abilities = msg | |
return True | |
@msghdlr(0x03) | |
def handle_chat(self, msg, source): | |
if source == 'server': | |
return True | |
txt = msg['chat_msg'] | |
if txt.startswith('/freecam back'): | |
if self.last_pos: | |
self.send_chat("Returning to last position") | |
self.to_client(self.last_pos) | |
self.safe = True | |
else: | |
self.send_chat("No saved position") | |
return False | |
elif txt.startswith('/freecam'): | |
if self.freecam and not self.safe: | |
self.send_chat("Please use '/freecam back' to go back " | |
"to a safe position") | |
return False | |
self.freecam = not self.freecam | |
if self.abilities: | |
if self.freecam: | |
new_abilities = self.abilities.copy() | |
new_abilities['allow_flying'] = True | |
self.to_client(new_abilities) | |
else: | |
# restore old ones | |
self.to_client(self.abilities) | |
self.send_chat("Freecam mode is now [%s]" % | |
('ON' if self.freecam else 'OFF')) | |
return False | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment