Skip to content

Instantly share code, notes, and snippets.

@Podshot
Created April 21, 2020 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Podshot/a3b3873324931460cc28cee00a891d72 to your computer and use it in GitHub Desktop.
Save Podshot/a3b3873324931460cc28cee00a891d72 to your computer and use it in GitHub Desktop.
from __future__ import annotations
from typing import TYPE_CHECKING, Tuple, Union
import numpy as np
import wx
from amulet_map_editor.amulet_wx.simple import SimpleDialog
from .construction import Construction, ConstructionSection
if TYPE_CHECKING:
from amulet.api.world import World
from amulet.api.selection import Selection
def in_bounds(
coordinates: Union[Tuple[int, int, int], Tuple[float, float, float]],
bounds: Tuple[slice, slice, slice],
) -> bool:
xs, ys, zs = bounds[0].start, bounds[1].start, bounds[2].start
xe, ye, ze = bounds[0].stop, bounds[1].stop, bounds[2].stop
x, y, z = coordinates
return xs <= x < xe and ys <= y < ye and zs <= z < ze
def export_construction(
world: "World", dimension: int, selection: "Selection", options: dict
):
collected_blocks = {}
def chunk_iter():
nonlocal collected_blocks
for chunk, slices, _ in world.get_chunk_slices(selection, dimension, False):
_blocks = chunk.blocks[slices].copy()
unique_blocks = np.unique(_blocks)
non_collected_blocks = set(unique_blocks) - set(collected_blocks.keys())
for not_collected in non_collected_blocks:
collected_blocks[not_collected] = world.palette[not_collected]
for i, key in enumerate(collected_blocks):
mask = np.where(_blocks == key)
_blocks[mask] = i
entities = []
tile_entities = []
yield ConstructionSection(slices[0].start, slices[1].start, slices[2].start, _blocks, entities, tile_entities)
construct = Construction.create_from(chunk_iter(), collected_blocks.values(), world.world_wrapper.platform, (0,0,0))
construct.save(options["file_path"])
print(options["file_path"])
construct_ = Construction.load(options["file_path"])
print(construct == construct_)
def show_ui(parent, world: "World", options: dict) -> dict:
options = {"file_path": "test.construction"}
dialog = SimpleDialog(parent, "Export Construction", wx.HORIZONTAL)
file_path_label = wx.StaticText(dialog.custom_panel, label="File Path:")
file_path_field = wx.TextCtrl(dialog.custom_panel, style=wx.TE_READONLY | wx.TE_RIGHT)
file_path_btn = wx.Button(dialog.custom_panel, label="Choose file destination")
def click_btn(event):
nonlocal options
with wx.FileDialog(dialog, "Save Construction", wildcard="Construction files (*.construction)|*.construction", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fd:
if fd.ShowModal() == wx.ID_CANCEL:
return
file_path = fd.GetPath()
options["file_path"] = file_path
file_path_field.SetValue(file_path)
event.Skip()
file_path_btn.Bind(wx.EVT_BUTTON, click_btn)
dialog.custom_panel.add_object(file_path_label, 0)
dialog.custom_panel.add_object(file_path_field, 0)
dialog.custom_panel.add_object(file_path_btn, 0)
dialog.Fit()
if dialog.ShowModal() == wx.ID_OK:
options["file_path"] = "test.construction"
return options
export = {
"v": 0,
"name": "Export Construction",
"features": ["src_selection", "wxoptions"],
"inputs": ["src_selection", "options"],
"operation": export_construction,
"wxoptions": show_ui,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment