# I'm just the one that executes the instructions!
import sys, math, json, operator, time
import mcpi.minecraft as minecraft
from PIL import Image as pillow
from blockid import get_block
import mcpi.block as block
import functions as pymc
from tqdm import tqdm
import tkinter as tk

# Functions

# Main code
mc = minecraft.Minecraft.create()
try:
    json_file = open("blocks.json")
    json_put = json.load(json_file)
except:
    pymc.chat(mc, "blocks.json not found, exiting!", 0)
    sys.exit(1)
try:
    rim = pillow.open(sys.argv[1])
except:
    pymc.chat(mc, "bad image, exiting!", 0)
    sys.exit(1)
orders = []
used = []

imwid, imhei = rim.size
if imhei > 200:
    maxheight = 200
    rim.thumbnail((200, maxheight), pillow.ANTIALIAS)
    imwid, imhei = rim.size
    pymc.chat(mc, "image is over 200 pixels, reducing height.", 1)

rim.convert('RGB')
im = rim.load()

pbar = tqdm(total=imhei*imwid)
for hei in range(imhei):
    for wid in range(imwid):
        smal = pymc.comp_pixel((im[wid, hei][0], im[wid, hei][1], im[wid, hei][2]), json_put)
        im[wid, hei] = smal[1]
        used.append(str(smal[2]))
        pbar.update(1)
pbar.close()

rim.save("result.GIF") # The result
json_file.close()

oldPos = mc.player.getPos()
playerPos = [round(oldPos.x), round(oldPos.y), round(oldPos.z)]
pymc.chat(mc, "Ready!")
pbar = tqdm(total=imhei*imwid)
num_temp = imhei*imwid-1
for hei in range(imhei):
    for wid in range(imwid):
        #print(used[wid + (imhei * hei)])
        gblock = get_block(used[num_temp])
        mc.setBlock(playerPos[0]+wid, playerPos[1]+hei, playerPos[2], gblock)
        num_temp -= 1
        pbar.update(1)
pbar.close()
pymc.chat(mc, "Done!!")
pymc.chat(mc, "Please star us on github if you like the result!", 2)

[((14, 5, 14, 33), 'mcpi.minecraft.Minecraft.create', 'minecraft.Minecraft.create', ({}, {}), '()', True, 'import mcpi.minecraft as minecraft\n'), ((39, 7, 39, 30), 'tqdm.tqdm', 'tqdm', (), '', False, 'from tqdm import tqdm\n'), ((53, 0, 53, 23), 'functions.chat', 'pymc.chat', ({(53, 10, 53, 12): 'mc', (53, 14, 53, 22): '"""Ready!"""'}, {}), "(mc, 'Ready!')", True, 'import functions as pymc\n'), ((54, 7, 54, 30), 'tqdm.tqdm', 'tqdm', (), '', False, 'from tqdm import tqdm\n'), ((64, 0, 64, 23), 'functions.chat', 'pymc.chat', ({(64, 10, 64, 12): 'mc', (64, 14, 64, 22): '"""Done!!"""'}, {}), "(mc, 'Done!!')", True, 'import functions as pymc\n'), ((65, 0, 65, 68), 'functions.chat', 'pymc.chat', ({(65, 10, 65, 12): 'mc', (65, 14, 65, 64): '"""Please star us on github if you like the result!"""', (65, 66, 65, 67): '(2)'}, {}), "(mc, 'Please star us on github if you like the result!', 2)", True, 'import functions as pymc\n'), ((17, 15, 17, 35), 'json.load', 'json.load', ({(17, 25, 17, 34): 'json_file'}, {}), '(json_file)', False, 'import sys, math, json, operator, time\n'), ((22, 10, 22, 34), 'PIL.Image.open', 'pillow.open', ({(22, 22, 22, 33): 'sys.argv[1]'}, {}), '(sys.argv[1])', True, 'from PIL import Image as pillow\n'), ((34, 4, 34, 66), 'functions.chat', 'pymc.chat', ({(34, 14, 34, 16): 'mc', (34, 18, 34, 62): '"""image is over 200 pixels, reducing height."""', (34, 64, 34, 65): '(1)'}, {}), "(mc, 'image is over 200 pixels, reducing height.', 1)", True, 'import functions as pymc\n'), ((19, 4, 19, 55), 'functions.chat', 'pymc.chat', ({(19, 14, 19, 16): 'mc', (19, 18, 19, 51): '"""blocks.json not found, exiting!"""', (19, 53, 19, 54): '(0)'}, {}), "(mc, 'blocks.json not found, exiting!', 0)", True, 'import functions as pymc\n'), ((20, 4, 20, 15), 'sys.exit', 'sys.exit', ({(20, 13, 20, 14): '(1)'}, {}), '(1)', False, 'import sys, math, json, operator, time\n'), ((24, 4, 24, 43), 'functions.chat', 'pymc.chat', ({(24, 14, 24, 16): 'mc', (24, 18, 24, 39): '"""bad image, exiting!"""', (24, 41, 24, 42): '(0)'}, {}), "(mc, 'bad image, exiting!', 0)", True, 'import functions as pymc\n'), ((25, 4, 25, 15), 'sys.exit', 'sys.exit', ({(25, 13, 25, 14): '(1)'}, {}), '(1)', False, 'import sys, math, json, operator, time\n'), ((42, 15, 42, 93), 'functions.comp_pixel', 'pymc.comp_pixel', ({(42, 31, 42, 82): '(im[wid, hei][0], im[wid, hei][1], im[wid, hei][2])', (42, 84, 42, 92): 'json_put'}, {}), '((im[wid, hei][0], im[wid, hei][1], im[wid, hei][2]), json_put)', True, 'import functions as pymc\n'), ((59, 17, 59, 42), 'blockid.get_block', 'get_block', ({(59, 27, 59, 41): 'used[num_temp]'}, {}), '(used[num_temp])', False, 'from blockid import get_block\n')]