# 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)

[((313, 341), 'mcpi.minecraft.Minecraft.create', 'minecraft.Minecraft.create', ([], {}), '()\n', (339, 341), True, 'import mcpi.minecraft as minecraft\n'), ((883, 908), 'tqdm.tqdm', 'tqdm', ([], {'total': '(imhei * imwid)'}), '(total=imhei * imwid)\n', (887, 908), False, 'from tqdm import tqdm\n'), ((1304, 1327), 'functions.chat', 'pymc.chat', (['mc', '"""Ready!"""'], {}), "(mc, 'Ready!')\n", (1313, 1327), True, 'import functions as pymc\n'), ((1335, 1360), 'tqdm.tqdm', 'tqdm', ([], {'total': '(imhei * imwid)'}), '(total=imhei * imwid)\n', (1339, 1360), False, 'from tqdm import tqdm\n'), ((1659, 1682), 'functions.chat', 'pymc.chat', (['mc', '"""Done!!"""'], {}), "(mc, 'Done!!')\n", (1668, 1682), True, 'import functions as pymc\n'), ((1683, 1751), 'functions.chat', 'pymc.chat', (['mc', '"""Please star us on github if you like the result!"""', '(2)'], {}), "(mc, 'Please star us on github if you like the result!', 2)\n", (1692, 1751), True, 'import functions as pymc\n'), ((398, 418), 'json.load', 'json.load', (['json_file'], {}), '(json_file)\n', (407, 418), False, 'import sys, math, json, operator, time\n'), ((514, 538), 'PIL.Image.open', 'pillow.open', (['sys.argv[1]'], {}), '(sys.argv[1])\n', (525, 538), True, 'from PIL import Image as pillow\n'), ((776, 838), 'functions.chat', 'pymc.chat', (['mc', '"""image is over 200 pixels, reducing height."""', '(1)'], {}), "(mc, 'image is over 200 pixels, reducing height.', 1)\n", (785, 838), True, 'import functions as pymc\n'), ((431, 482), 'functions.chat', 'pymc.chat', (['mc', '"""blocks.json not found, exiting!"""', '(0)'], {}), "(mc, 'blocks.json not found, exiting!', 0)\n", (440, 482), True, 'import functions as pymc\n'), ((487, 498), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (495, 498), False, 'import sys, math, json, operator, time\n'), ((551, 590), 'functions.chat', 'pymc.chat', (['mc', '"""bad image, exiting!"""', '(0)'], {}), "(mc, 'bad image, exiting!', 0)\n", (560, 590), True, 'import functions as pymc\n'), ((595, 606), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (603, 606), False, 'import sys, math, json, operator, time\n'), ((976, 1054), 'functions.comp_pixel', 'pymc.comp_pixel', (['(im[wid, hei][0], im[wid, hei][1], im[wid, hei][2])', 'json_put'], {}), '((im[wid, hei][0], im[wid, hei][1], im[wid, hei][2]), json_put)\n', (991, 1054), True, 'import functions as pymc\n'), ((1497, 1522), 'blockid.get_block', 'get_block', (['used[num_temp]'], {}), '(used[num_temp])\n', (1506, 1522), False, 'from blockid import get_block\n')]