milo: item rewrite + ability to remove nbts from recipes
This commit is contained in:
@@ -6,18 +6,14 @@ local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
local device = _G.device
|
||||
|
||||
local context = Milo:getContext()
|
||||
|
||||
local page = UI.Page {
|
||||
titleBar = UI.TitleBar {
|
||||
title = 'Limit Resource',
|
||||
previousPage = true,
|
||||
event = 'form_cancel',
|
||||
},
|
||||
local manageTab = UI.Window {
|
||||
tabTitle = 'Manage',
|
||||
form = UI.Form {
|
||||
x = 1, y = 2, height = 10, ex = -1,
|
||||
x = 1, ex = -1, ey = -1,
|
||||
--manualControls = true,
|
||||
[1] = UI.TextEntry {
|
||||
formLabel = 'Name', formKey = 'displayName', help = 'Override display name',
|
||||
shadowText = 'Display name',
|
||||
@@ -42,100 +38,80 @@ local page = UI.Page {
|
||||
formLabel = 'Ignore NBT', formKey = 'ignoreNbtHash',
|
||||
help = 'Ignore NBT of item',
|
||||
},
|
||||
machineButton = UI.Button {
|
||||
x = 2, y = -2, width = 10,
|
||||
formLabel = 'Machine',
|
||||
event = 'select_machine',
|
||||
text = 'Assign',
|
||||
},
|
||||
infoButton = UI.Button {
|
||||
x = 2, y = -2,
|
||||
event = 'show_info',
|
||||
text = 'Info',
|
||||
},
|
||||
resetButton = UI.Button {
|
||||
x = 9, y = -2,
|
||||
event = 'reset',
|
||||
text = 'Reset',
|
||||
help = 'Clear recipe and all settings',
|
||||
},
|
||||
},
|
||||
rsControl = UI.SlideOut {
|
||||
backgroundColor = colors.cyan,
|
||||
titleBar = UI.TitleBar {
|
||||
title = "Redstone Control",
|
||||
},
|
||||
form = UI.Form {
|
||||
y = 2,
|
||||
[1] = UI.Chooser {
|
||||
width = 7,
|
||||
formLabel = 'RS Control', formKey = 'rsControl',
|
||||
nochoice = 'No',
|
||||
choices = {
|
||||
{ name = 'Yes', value = true },
|
||||
{ name = 'No', value = false },
|
||||
},
|
||||
help = 'Control via redstone'
|
||||
},
|
||||
[2] = UI.Chooser {
|
||||
width = 25,
|
||||
formLabel = 'RS Device', formKey = 'rsDevice',
|
||||
--choices = devices,
|
||||
help = 'Redstone Device'
|
||||
},
|
||||
[3] = UI.Chooser {
|
||||
width = 10,
|
||||
formLabel = 'RS Side', formKey = 'rsSide',
|
||||
--nochoice = 'No',
|
||||
choices = {
|
||||
{ name = 'up', value = 'up' },
|
||||
{ name = 'down', value = 'down' },
|
||||
{ name = 'east', value = 'east' },
|
||||
{ name = 'north', value = 'north' },
|
||||
{ name = 'west', value = 'west' },
|
||||
{ name = 'south', value = 'south' },
|
||||
},
|
||||
help = 'Output side'
|
||||
},
|
||||
}
|
||||
|
||||
local machinesTab = UI.Window {
|
||||
tabTitle = 'Machine',
|
||||
backgroundColor = colors.cyan,
|
||||
grid = UI.ScrollingGrid {
|
||||
x = 2, ex = -2, y = 2, ey = -2,
|
||||
disableHeader = true,
|
||||
columns = {
|
||||
{ heading = 'Name', key = 'displayName'},
|
||||
},
|
||||
sortColumn = 'displayName',
|
||||
help = 'Double-click to set machine',
|
||||
},
|
||||
machines = UI.SlideOut {
|
||||
backgroundColor = colors.cyan,
|
||||
titleBar = UI.TitleBar {
|
||||
title = 'Select Machine',
|
||||
event = 'cancel_machine',
|
||||
}
|
||||
|
||||
local recipeTab = UI.Window {
|
||||
tabTitle = 'Recipe',
|
||||
backgroundColor = colors.cyan,
|
||||
grid = UI.ScrollingGrid {
|
||||
x = 2, ex = -2, y = 2, ey = -4,
|
||||
disableHeader = true,
|
||||
columns = {
|
||||
{ heading = 'Slot', key = 'slot', width = 2 },
|
||||
{ heading = 'Key', key = 'key' },
|
||||
},
|
||||
grid = UI.ScrollingGrid {
|
||||
y = 2, ey = -5,
|
||||
disableHeader = true,
|
||||
columns = {
|
||||
{ heading = 'Name', key = 'displayName'},
|
||||
},
|
||||
sortColumn = 'displayName',
|
||||
},
|
||||
button1 = UI.Button {
|
||||
x = -14, y = -3,
|
||||
text = 'Ok', event = 'set_machine',
|
||||
},
|
||||
button2 = UI.Button {
|
||||
x = -9, y = -3,
|
||||
text = 'Cancel', event = 'cancel_machine',
|
||||
},
|
||||
statusBar = UI.StatusBar { values = 'Enter or double click to select' },
|
||||
sortColumn = 'slot',
|
||||
},
|
||||
info = UI.SlideOut {
|
||||
titleBar = UI.TitleBar {
|
||||
title = "Information",
|
||||
},
|
||||
textArea = UI.TextArea {
|
||||
x = 2, ex = -2, y = 3, ey = -4,
|
||||
backgroundColor = colors.black,
|
||||
},
|
||||
cancel = UI.Button {
|
||||
ex = -2, y = -2, width = 6,
|
||||
text = 'Okay',
|
||||
event = 'hide_info',
|
||||
},
|
||||
ignoreNBT = UI.Button {
|
||||
x = -13, y = -2,
|
||||
text = 'Ignore NBT', event = 'ignore_nbt',
|
||||
},
|
||||
}
|
||||
|
||||
local infoTab = UI.Window {
|
||||
tabTitle = 'Info',
|
||||
backgroundColor = colors.cyan,
|
||||
textArea = UI.TextArea {
|
||||
x = 2, ex = -2, y = 2,
|
||||
},
|
||||
}
|
||||
|
||||
local resetTab = UI.Window {
|
||||
tabTitle = 'Reset',
|
||||
backgroundColor = colors.cyan,
|
||||
textArea = UI.TextArea {
|
||||
y = 2, ey = 6,
|
||||
textColor = colors.yellow,
|
||||
value = [[ Warning!
|
||||
|
||||
This will clear all setting,
|
||||
recipe, and machine for this item.]]
|
||||
},
|
||||
resetButton = UI.Button {
|
||||
x = 17, y = 7,
|
||||
event = 'reset',
|
||||
text = 'Reset',
|
||||
help = 'Clear recipe and all settings',
|
||||
},
|
||||
}
|
||||
|
||||
local page = UI.Page {
|
||||
titleBar = UI.TitleBar {
|
||||
title = 'Item settings',
|
||||
previousPage = true,
|
||||
},
|
||||
tabs = UI.Tabs {
|
||||
y = 2, ey = -2,
|
||||
[1] = manageTab,
|
||||
[2] = recipeTab,
|
||||
[3] = machinesTab,
|
||||
[4] = infoTab,
|
||||
[5] = resetTab,
|
||||
},
|
||||
statusBar = UI.StatusBar { },
|
||||
notification = UI.Notification { },
|
||||
@@ -146,17 +122,19 @@ function page:enable(item)
|
||||
self.item = Util.shallowCopy(item)
|
||||
self.res = item.resource or { }
|
||||
self.res.displayName = self.item.displayName
|
||||
self.form:setValues(self.res)
|
||||
self.titleBar.title = item.displayName or item.name
|
||||
manageTab.form:setValues(self.res)
|
||||
|
||||
local machine = Craft.machineLookup[self.item.key]
|
||||
self.form.machineButton.inactive = not machine
|
||||
if machine then
|
||||
self:filterMachines(machine)
|
||||
end
|
||||
|
||||
self.tabs:selectTab(manageTab)
|
||||
|
||||
self.tabs:setActive(machinesTab, machine)
|
||||
self.tabs:setActive(recipeTab, Craft.findRecipe(item))
|
||||
|
||||
UI.Page.enable(self)
|
||||
self:focusFirst()
|
||||
end
|
||||
|
||||
function page:filterMachines(machine)
|
||||
@@ -165,65 +143,105 @@ function page:filterMachines(machine)
|
||||
return node.adapter and node.adapter.online and node.adapter.pushItems
|
||||
end
|
||||
end)
|
||||
self.machines.grid:setValues(t)
|
||||
self.machines.grid:setSelected('name', machine)
|
||||
machinesTab.grid:setValues(t)
|
||||
machinesTab.grid:setSelected('name', machine)
|
||||
end
|
||||
|
||||
function page.machines.grid:getDisplayValues(row)
|
||||
function machinesTab.grid:getDisplayValues(row)
|
||||
row = Util.shallowCopy(row)
|
||||
row.displayName = row.displayName or row.name
|
||||
return row
|
||||
end
|
||||
|
||||
function page.machines.grid:getRowTextColor(row, selected)
|
||||
function machinesTab.grid:getRowTextColor(row, selected)
|
||||
if row.name == Craft.machineLookup[page.item.key] then
|
||||
return colors.yellow
|
||||
end
|
||||
return UI.Grid:getRowTextColor(row, selected)
|
||||
end
|
||||
|
||||
function page.rsControl:enable()
|
||||
local devices = self.form[2].choices
|
||||
Util.clear(devices)
|
||||
for _,dev in pairs(device) do
|
||||
if dev.setOutput then
|
||||
table.insert(devices, { name = dev.name, value = dev.name })
|
||||
end
|
||||
end
|
||||
function machinesTab:eventHandler(event)
|
||||
if event.type == 'grid_select' then
|
||||
Craft.machineLookup[page.item.key] = event.selected.name
|
||||
Util.writeTable(Craft.MACHINE_LOOKUP, Craft.machineLookup)
|
||||
|
||||
if Util.size(devices) == 0 then
|
||||
table.insert(devices, { name = 'None found', values = '' })
|
||||
end
|
||||
self.grid:draw()
|
||||
page.notification:info('Machine saved')
|
||||
|
||||
UI.SlideOut.enable(self)
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
function page.rsControl:eventHandler(event)
|
||||
if event.type == 'form_cancel' then
|
||||
self:hide()
|
||||
elseif event.type == 'form_complete' then
|
||||
self:hide()
|
||||
else
|
||||
return UI.SlideOut.eventHandler(self, event)
|
||||
function recipeTab:enable()
|
||||
self.recipe = Craft.findRecipe(page.item)
|
||||
|
||||
local t = { }
|
||||
if self.recipe then
|
||||
for k, v in pairs(self.recipe.ingredients) do
|
||||
table.insert(t, {
|
||||
slot = k,
|
||||
key = v,
|
||||
})
|
||||
end
|
||||
end
|
||||
return true
|
||||
self.grid:setValues(t)
|
||||
UI.Window.enable(self)
|
||||
end
|
||||
|
||||
function recipeTab:eventHandler(event)
|
||||
if event.type == 'ignore_nbt' then
|
||||
local selected = self.grid:getSelected()
|
||||
local item = itemDB:splitKey(selected.key)
|
||||
item.nbtHash = nil
|
||||
selected.key = itemDB:makeKey(item)
|
||||
self.grid:draw()
|
||||
|
||||
self.recipe.ingredients = { }
|
||||
for _, v in pairs(self.grid.values) do
|
||||
self.recipe.ingredients[v.slot] = v.key
|
||||
end
|
||||
|
||||
Milo:updateRecipe(self.recipe.result, self.recipe)
|
||||
page.notification:info('Recipe updated')
|
||||
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
function infoTab:draw()
|
||||
local item = page.item
|
||||
local value =
|
||||
string.format('%s%s%s\n%s\n',
|
||||
Ansi.orange, item.displayName, Ansi.reset,
|
||||
item.name)
|
||||
|
||||
if item.nbtHash then
|
||||
value = value .. item.nbtHash .. '\n'
|
||||
end
|
||||
|
||||
value = value .. string.format('\n%sDamage:%s %s',
|
||||
Ansi.yellow, Ansi.reset, item.damage)
|
||||
|
||||
if item.maxDamage and item.maxDamage > 0 then
|
||||
value = value .. string.format(' (max: %s)', item.maxDamage)
|
||||
end
|
||||
|
||||
if item.maxCount then
|
||||
value = value .. string.format('\n%sStack Size: %s%s',
|
||||
Ansi.yellow, Ansi.reset, item.maxCount)
|
||||
end
|
||||
|
||||
self.textArea.value = value
|
||||
UI.Window.draw(self)
|
||||
end
|
||||
|
||||
function page:eventHandler(event)
|
||||
if event.type == 'form_cancel' then
|
||||
UI:setPreviousPage()
|
||||
|
||||
elseif event.type == 'show_rs' then
|
||||
self.rsControl:show()
|
||||
|
||||
elseif event.type == 'select_machine' then
|
||||
self.machines:show()
|
||||
|
||||
elseif event.type == 'reset' then
|
||||
if context.userRecipes[self.item.key] then
|
||||
context.userRecipes[self.item.key] = nil
|
||||
Util.writeTable(Craft.USER_RECIPES, context.userRecipes)
|
||||
Craft.loadRecipes()
|
||||
Milo:updateRecipe(self.item.key, nil)
|
||||
end
|
||||
|
||||
if context.resources[self.item.key] then
|
||||
@@ -238,47 +256,8 @@ function page:eventHandler(event)
|
||||
|
||||
UI:setPreviousPage()
|
||||
|
||||
elseif event.type == 'grid_select' then
|
||||
Craft.machineLookup[self.item.key] = event.selected.name
|
||||
self.machines.grid:draw()
|
||||
|
||||
elseif event.type == 'set_machine' then
|
||||
local machine = self.machines.grid:getSelected()
|
||||
if machine then
|
||||
Util.writeTable(Craft.MACHINE_LOOKUP, Craft.machineLookup)
|
||||
end
|
||||
self.machines:hide()
|
||||
|
||||
elseif event.type == 'cancel_machine' then
|
||||
self.machines:hide()
|
||||
|
||||
elseif event.type == 'show_info' then
|
||||
local value =
|
||||
string.format('%s%s%s\n%s\n',
|
||||
Ansi.orange, self.item.displayName, Ansi.reset,
|
||||
self.item.name)
|
||||
|
||||
if self.item.nbtHash then
|
||||
value = value .. self.item.nbtHash .. '\n'
|
||||
end
|
||||
|
||||
value = value .. string.format('\n%sDamage:%s %s',
|
||||
Ansi.yellow, Ansi.reset, self.item.damage)
|
||||
|
||||
if self.item.maxDamage and self.item.maxDamage > 0 then
|
||||
value = value .. string.format(' (max: %s)', self.item.maxDamage)
|
||||
end
|
||||
|
||||
if self.item.maxCount then
|
||||
value = value .. string.format('\n%sStack Size: %s%s',
|
||||
Ansi.yellow, Ansi.reset, self.item.maxCount)
|
||||
end
|
||||
|
||||
self.info.textArea.value = value
|
||||
self.info:show()
|
||||
|
||||
elseif event.type == 'hide_info' then
|
||||
self.info:hide()
|
||||
elseif event.type == 'tab_activate' then
|
||||
event.activated:focusFirst()
|
||||
|
||||
elseif event.type == 'form_invalid' then
|
||||
self.notification:error(event.message)
|
||||
@@ -290,7 +269,7 @@ function page:eventHandler(event)
|
||||
elseif event.type == 'form_complete' then
|
||||
local item = self.item
|
||||
|
||||
if self.form:save() then
|
||||
if manageTab.form:save() then
|
||||
if self.res.displayName ~= self.origItem.displayName then
|
||||
self.origItem.displayName = self.res.displayName
|
||||
itemDB:add(self.origItem)
|
||||
|
||||
Reference in New Issue
Block a user