From 22e62b7004cb6e19056a2d1382ab7bf84ae6e418 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Mon, 24 Oct 2022 21:56:53 +0200 Subject: add portal addon --- BerryClient/addons/portal.py | 119 +++++++++++++++++++++++++++++++++++++++++++ BerryClient/core.py | 2 + BerryClient/events.py | 2 +- 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 BerryClient/addons/portal.py diff --git a/BerryClient/addons/portal.py b/BerryClient/addons/portal.py new file mode 100644 index 0000000..642c1c4 --- /dev/null +++ b/BerryClient/addons/portal.py @@ -0,0 +1,119 @@ +import math +import logging + +from mcpi.block import LAPIS_LAZULI_BLOCK + +from BerryClient.commands import command +from BerryClient.events import on_block_hit, on_player_movement + +log = logging.getLogger(__name__) + +previous_portal_blocks = [ + None, + None +] + +portals = [ + None, # Blue + None, # Orange +] +portal_index = 0 + +can_teleport = {} + +@command() +def resetPortals(core): + global previous_portal_blocks + global portals + + for i, portal in enumerate(portals): + a, b, c = portal + core.setBlock(a, b-1, c, previous_portal_blocks[i]) + + previous_portal_blocks = [None, None] + portals = [None, None] + +@on_block_hit() +def set_portal(core, event): + global portal_index + + x, y, z = event.pos + + previous_portal_block = previous_portal_blocks[portal_index] + if previous_portal_block: + a, b, c = portals[portal_index] + core.setBlock(a, b-1, c, previous_portal_block) + + previous_portal_blocks[portal_index] = core.getBlock(x, y, z) + core.setBlock(x, y, z, LAPIS_LAZULI_BLOCK) + + portals[portal_index] = (x+0.5, y+1, z+0.5) + portal_index = (portal_index+1) % len(portals) + + core.postToChat("Set Portal") + +@on_player_movement() +def on_move(core, playerId, old_pos, new_pos): + if can_teleport.get(playerId, True): + for portalId, portal in enumerate(portals): + if not portal: + continue + + x, y, z = new_pos + in_x, in_y, in_z = portal + + offset_x = x - in_x + offset_y = y - in_y + offset_z = z - in_z + + offsets = [ + abs(offset_x), + abs(offset_y), + abs(offset_z) + ] + in_portal = True + for offset in offsets: + if offset > 0.6: + in_portal = False + break + + + if in_portal and portals[not portalId]: + x, y, z = portals[not portalId] + x += offset_x + z += offset_z + + can_teleport[playerId] = False + core.entity.setPos(playerId, x, y, z) + break + + else: + in_portal_count = 0 + + for portalId, portal in enumerate(portals): + if not portal: + continue + + x, y, z = new_pos + in_x, in_y, in_z = portal + + offset_x = x - in_x + offset_y = y - in_y + offset_z = z - in_z + + offsets = [ + abs(offset_x), + abs(offset_y), + abs(offset_z) + ] + in_portal = True + for offset in offsets: + if offset > 0.7: + in_portal = False + break + + if in_portal: + in_portal_count += 1 + + if in_portal_count == 0: + can_teleport[playerId] = True diff --git a/BerryClient/core.py b/BerryClient/core.py index 28e2a3b..afe26c1 100644 --- a/BerryClient/core.py +++ b/BerryClient/core.py @@ -15,6 +15,8 @@ class BerryCore: "BerryClient.addons.core", "BerryClient.addons.blockworld", + + #"BerryClient.addons.portal", ] ADDON_MODULES = {} diff --git a/BerryClient/events.py b/BerryClient/events.py index 041a852..1b75e24 100644 --- a/BerryClient/events.py +++ b/BerryClient/events.py @@ -62,7 +62,7 @@ def process_events(core: "BerryCore"): for hit in core.events.pollBlockHits(): for callback in BLOCK_CALLBACKS: - if callback(block, hit): + if callback(core, hit): break for post in core.events.pollChatPosts(): -- cgit v1.2.3