diff --git a/custom.css b/custom.css index 294eeb6f..bb6c5143 100644 --- a/custom.css +++ b/custom.css @@ -23,7 +23,7 @@ background-color: #eee !important; } -.card.term, .card.terms { +.card.bg-info .card.bg-info { background-color: #7cb6bf !important; } diff --git a/index.html b/index.html index c3ab726c..7fa074b5 100644 --- a/index.html +++ b/index.html @@ -36,7 +36,7 @@ -
+
@@ -63,67 +63,67 @@
-
+
- +
-
+
Rolls
- + Min - + Max - + n - + p - +
-
+
Bonus Rolls
- + Min - + Max - + n - + p - +
-
+
- +
-
+
Type
- @@ -134,40 +134,40 @@
-
+
Name
- +
-
+
Weight
- +
-
+
Quality
- +
-
+
- +
-
+
Function
- @@ -187,53 +187,53 @@
-
+
Count
- + Min - + Max - + n - + p - +
-
+
Damage
- + Min - + Max - + n - + p - +
-
+
- Tag + NBT
- +
-
+
Optional Enchantments @@ -284,95 +284,84 @@
-
+
Levels
- + Min - + Max - + n - + p - +
-
+
- +
-
+
Limit
- +
-
+
Source
-
-
+
Name
- +
-
+
limit - -
- - Min - - Max - - n - - p - + Min + + Max +
-
+
-
+
- +
-
+
Entity
- @@ -381,52 +370,52 @@
-
+
Enchantment
- +
-
+
Formula
-
-
+
Multiplier
- +
-
+
Extra
- +
-
+
Propability
- +
-
+
- +
-
+
Attribute
- @@ -442,44 +431,44 @@
-
+
Name
- +
-
+
Amount
- + Min - + Max - + n - + p - +
-
+
Operation
-
-
+
Slots
@@ -500,28 +489,28 @@
-
+
- +
-
+
Source
- +
-
+
Target
- +
-
+
Operation
- @@ -529,16 +518,16 @@
-
+
- +
-
+
Condition
- @@ -555,29 +544,29 @@
-
+
Chance
- +
-
+
Looting Multiplier
- +
-
+
- +
-
+
Entity
- @@ -597,11 +586,11 @@
-
+
Block
- +
@@ -614,31 +603,31 @@
-
+
Raining
- - + +
-
+
Thundering
- - + +
-
+
Enchantment
- +
-
+
Chances
@@ -646,7 +635,7 @@
-
+
@@ -673,53 +662,53 @@
-
+
-
+
X
- Min - - Max - + Min + + Max +
-
+
Y
- Min - - Max - + Min + + Max +
-
+
Z
Min - + Max - +
-
+
Biome
- +
-
+
Feature
- +
-
+
Dimension
- @@ -728,24 +717,24 @@
-
+
-
+
Type
- +
-
+
NBT
- +
-
+
@@ -761,162 +750,162 @@ Tag
-
+
Count
- + Min - + Max - +
-
+
Durability
- + Min - + Max - +
-
+
Potion
- +
-
+
NBT
- +
-
+
-
+
Projectile
- - + +
-
+
Explosion
- - + +
-
+
Fire
- - + +
-
+
Magic
- - + +
-
+
Lightning
- - + +
-
+
Starvation
- - + +
-
+
Void
- - + +
-
+
Bypass Armor
- - + +
-
+
Dealt
- + Min - + Max - +
-
+
Taken
- + Min - + Max - +
-
+
Blocked
- - + +
diff --git a/model.js b/model.js index 753fd5d1..612432f2 100644 --- a/model.js +++ b/model.js @@ -6,7 +6,6 @@ $('#indentationSelect').val("2"); let indentation = 2; let luck_based = false; -const nodes = '.loot-table, .pool, .entry, .child, .term, .terms, .function, .condition, .modifier, .operation, .predicate, .location, .source-entity, .direct-entity'; let table = { type: "minecraft:generic", pools: [] @@ -77,46 +76,64 @@ function copySource(el) { } function getParent(el) { - let $parent = $(el).closest(nodes); - let index = $parent.attr('data-index'); - if ($parent.hasClass('loot-table')) { + let $node = $(el).closest('[data-field]'); + let fields = $node.attr('data-field').split('.'); + if (fields.length === 1 && fields[0] === 'table') { return table; - } else if ($parent.hasClass('pool')) { - return getParent($parent.parent()).pools[index]; - } else if ($parent.hasClass('entry')) { - return getParent($parent.parent()).entries[index]; - } else if ($parent.hasClass('child')) { - return getParent($parent.parent()).children[index]; - } else if ($parent.hasClass('term')) { - return getParent($parent.parent()).term; - } else if ($parent.hasClass('terms')) { - return getParent($parent.parent()).terms[index]; - } else if ($parent.hasClass('function')) { - return getParent($parent.parent()).functions[index]; - } else if ($parent.hasClass('condition')) { - return getParent($parent.parent()).conditions[index]; - } else if ($parent.hasClass('modifier')) { - return getParent($parent.parent()).modifiers[index]; - } else if ($parent.hasClass('operation')) { - return getParent($parent.parent()).ops[index]; - } else if ($parent.hasClass('predicate')) { - return getParent($parent.parent()).predicate; - } else if ($parent.hasClass('location')) { - return getParent($parent.parent()).location; - } else if ($parent.hasClass('source-entity')) { - return getParent($parent.parent()).source_entity; - } else if ($parent.hasClass('direct-entity')) { - return getParent($parent.parent()).direct_entity; } + if ($node.attr('data-type')) { + fields = fields.slice(0, -1); + } + let node = getParent($node.parent()); + for (let f of fields) { + if (f.endsWith('[]')) { + f = f.slice(0, -2); + let index = $node.attr('data-index'); + node = node[f][index]; + } else { + if (node[f] === undefined) { + node[f] = {}; + } + node = node[f]; + } + } + return node; } function getSuperParent(el) { - let $parent = $(el).closest(nodes); + let $parent = $(el).closest('[data-field]'); return getParent($parent.parent()); } +function setField(node, field, value) { + let fields = field.split('.'); + let last = fields.splice(-1)[0]; + for (let f of fields) { + node = node[f]; + } + node[last] = value; +} + +function deleteField(node, field) { + let fields = field.split('.'); + let last = fields.splice(-1)[0]; + for (let f of fields) { + node = node[f]; + } + delete node[last]; +} + +function getField(node, field) { + let fields = field.split('.'); + let last = fields.splice(-1)[0]; + for (let f of fields) { + node = node[f]; + } + return node[last]; +} + function getIndex(el) { - let $parent = $(el).closest(nodes); + let $parent = $(el).closest('[data-field]'); return parseInt($parent.attr('data-index')); } @@ -130,16 +147,6 @@ function addPool(el) { invalidated(); } -function removePool(el) { - let parent = getSuperParent(el); - let index = getIndex(el); - parent.pools.splice(index, 1); - if (parent.pools.length === 0) { - delete parent.pools; - } - invalidated(); -} - function addEntry(el) { let pool = getParent(el); if (!pool.entries) { @@ -152,16 +159,6 @@ function addEntry(el) { invalidated(); } -function removeEntry(el) { - let parent = getSuperParent(el); - let index = getIndex(el); - parent.entries.splice(index, 1); - if (parent.entries.length === 0) { - delete parent.entries; - } - invalidated(); -} - function addChild(el) { let entry = getParent(el); if (!entry.children) { @@ -174,16 +171,6 @@ function addChild(el) { invalidated(); } -function removeChild(el) { - let parent = getSuperParent(el); - let index = getIndex(el); - parent.children.splice(index, 1); - if (parent.children.length === 0) { - delete parent.children; - } - invalidated(); -} - function addFunction(el) { let entry = getParent(el); if (!entry.functions) { @@ -195,15 +182,6 @@ function addFunction(el) { invalidated(); } -function removeFunction(el) { - let parent = getSuperParent(el); - parent.functions.splice(getIndex(el), 1); - if (parent.functions.length === 0) { - delete parent.functions; - } - invalidated(); -} - function addCondition(el) { let parent = getParent(el); if (!parent.conditions) { @@ -216,161 +194,115 @@ function addCondition(el) { invalidated(); } -function removeCondition(el) { - let parent = getSuperParent(el); - if (parent.conditions) { - parent.conditions.splice(getIndex(el), 1); - if (parent.conditions.length === 0) { - delete parent.conditions; +function updateField(el) { + let $field = $(el).closest('[data-field]'); + let fields = $field.attr('data-field'); + let field = fields.split('.').slice(-1)[0]; + let type = $field.attr('data-type'); + let node = getParent(el); + let value = undefined; + + if (type === 'string' || type === 'int' || type === 'float' || type === 'enum' || type === 'json' || type === 'nbt') { + value = $(el).val(); + } + if (type === 'int') { + value = parseInt(value); + if (isNaN(value)) { + value = ''; } - } else { - parent.terms.splice(getIndex(el), 1); - if (parent.terms.length === 0) { - delete parent.terms; + } else if (type === 'float') { + value = parseFloat(value); + if (isNaN(value)) { + value = ''; } + } else if (type === 'nbt') { + + if (!value.startsWith('{')) { + value = '{' + value; + } + if (!value.endsWith('}')) { + value = value + '}'; + } + } else if (type === 'json') { + value = parseJSONValue(value) + } else if (type === 'range') { + value = getRangeValue($field, node[field]); + } else if (type === 'checkbox') { + value = $(el).prop('checked'); + } else if (type === 'boolean') { + value = getBooleanValue(node[field], ($(el).val() === 'true')); } - invalidated(); -} - -function updateValue(root, field, value) { - let f = field.split('.'); - if (f.length === 1) return root[f[0]] = value; - if (!root[f[0]]) root[f[0]] = {}; - if (f.length === 2) return root[f[0]][f[1]] = value; - if (!root[f[0]][f[1]]) root[f[0]][f[1]] = {}; - if (f.length === 3) return root[f[0]][f[1]][f[2]] = value; - if (!root[f[0]][f[1]][f[2]]) root[f[0]][f[1]][f[2]] = {}; - if (f.length === 4) return root[f[0]][f[1]][f[2]][f[3]] = value; - if (!root[f[0]][f[1]][f[2]][f[3]]) root[f[0]][f[1]][f[2]][f[3]] = {}; - if (f.length === 5) return root[f[0]][f[1]][f[2]][f[3]][f[4]] = value; - if (!root[f[0]][f[1]][f[2]][f[3]][f[4]]) root[f[0]][f[1]][f[2]][f[3]][f[4]] = {}; - if (f.length === 6) return root[f[0]][f[1]][f[2]][f[3]][f[4]][f[5]] = value; -} - -function deleteValue(root, field) { - let f = field.split('.'); - if (f.length === 1) delete root[f[0]]; - if (f.length === 2) delete root[f[0]][f[1]]; - if (f.length === 3) delete root[f[0]][f[1]][f[2]]; - if (f.length === 4) delete root[f[0]][f[1]][f[2]][f[3]]; - if (f.length === 5) delete root[f[0]][f[1]][f[2]][f[3]][f[4]]; - if (f.length === 6) delete root[f[0]][f[1]][f[2]][f[3]][f[4]][f[6]]; -} - -function getValue(root, field) { - let f = field.split('.'); - if (f.length === 1) return root[f[0]]; - if (f.length === 2) return root[f[0]][f[1]]; - if (f.length === 3) return root[f[0]][f[1]][f[2]]; - if (f.length === 4) return root[f[0]][f[1]][f[2]][f[3]]; - if (f.length === 5) return root[f[0]][f[1]][f[2]][f[3]][f[4]]; - if (f.length === 6) return root[f[0]][f[1]][f[2]][f[3]][f[4]][f[6]]; -} - -function updateField(el, field) { - updateValue(getParent(el), field, $(el).val()); - invalidated(); -} - -function updateJSONField(el, field) { - let value = parseJSONValue($(el).val()); - updateValue(getParent(el), field, value); - invalidated(); -} - -function updateIntField(el, field) { - let value = parseInt($(el).val()); - if (isNaN(value)) { - deleteValue(getParent(el), field); + if (value === '') { + deleteField(node, field); } else { - updateValue(getParent(el), field, value); + setField(node, field, value); } invalidated(); } -function updateFloatField(el, field) { - let value = parseFloat($(el).val()); - if (isNaN(value)) { - deleteValue(getParent(el), field); - } else { - updateValue(getParent(el), field, value); +function removeField(el) { + let node = getSuperParent(el); + let $field = $(el).closest('[data-field]'); + let index = $field.attr('data-index'); + let last = $field.attr('data-field').slice(0, -2); + node[last].splice(index, 1); + if (node[last].length === 0) { + delete node[last]; } invalidated(); } -function updateCheckedField(el, field) { - getParent(el)[field] = $(el).prop('checked'); - invalidated(); -} - -function updateRangeType(el, field, type) { +function updateRangeType(el) { + let $field = $(el).closest('[data-field]'); + let field = $field.attr('data-field'); + let type = $(el).attr('value'); if (type === 'range') { - updateValue(getParent(el), field, {}); + setField(getParent(el), field, {}); } else if (type === 'binomial') { - updateValue(getParent(el), field, {type: "minecraft:binomial"}); + setField(getParent(el), field, {type: "minecraft:binomial"}); } else { - updateValue(getParent(el), field, 0); + setField(getParent(el), field, 0); } - updateRangeField(el, field); + updateField(el); } -function updateRangeField(el, field) { - let parent = getParent(el); - let data = getValue(parent, field); - let $range = $(el).closest('[data-type="range"]'); +function getRangeValue($field, data) { + console.log(data); if (typeof data === 'object') { + console.log('object'); if (data.type && data.type.match(/(minecraft:)?binomial/)) { - let n = $range.find('.binomial.n').val(); - let p = $range.find('.binomial.p').val(); - if (n) { - data.n = parseInt(n); - } else { - delete data.n; - } - if (p) { - data.p = parseFloat(p); - } else { - delete data.min; - } + let n = $field.find('.binomial.n').val(); + let p = $field.find('.binomial.p').val(); + if (n) data.n = parseInt(n); + else delete data.n; + if (p) data.p = parseFloat(p); + else delete data.p; } else { - let min = $range.find('.range.min').val(); - let max = $range.find('.range.max').val(); - if (min) { - data.min = parseFloat(min); - } else { - delete data.min; - } - if (max) { - data.max = parseFloat(max); - } else { - delete data.max; - } + let min = $field.find('.range.min').val(); + let max = $field.find('.range.max').val(); + if (min) data.min = parseFloat(min); + else delete data.min; + if (max) data.max = parseFloat(max); + else delete data.max; } } else { - data = parseFloat($range.find('.exact').val()); + console.log('else'); + data = parseFloat($field.find('.exact').val()); + if (isNaN(data)) { + data = ''; + } } - updateValue(parent, field, data); - invalidated(); + return data; } -function updateRadioField(el, field) { - let parent = getParent(el); - let value = $(el).val(); - let oldvalue = getValue(parent, field); - if (value === 'true') { - if (oldvalue === true) { - deleteValue(parent, field); - } else { - updateValue(getParent(el), field, true); - } - } else if (value === 'false') { - if (oldvalue === false) { - deleteValue(parent, field); - } else { - updateValue(getParent(el), field, false); - } +function getBooleanValue(oldvalue, newvalue) { + if (oldvalue === newvalue) { + return ''; + } else if (newvalue) { + return true; + } else { + return false; } - invalidated(); } function addEnchantment(el) { @@ -419,22 +351,19 @@ function removeModifier(el) { function addModifierSlot(el) { let modifier = getParent(el); - if (!modifier.slots) { - modifier.slots = []; + if (!modifier.slot) { + modifier.slot = []; } - modifier.slots.push($(el).attr('data-slot')); + modifier.slot.push($(el).attr('data-slot')); invalidated(); } function removeModifierSlot(el) { let modifier = getParent(el); let slot = $(el).attr('data-slot'); - let index = modifier.slots.indexOf(slot); + let index = modifier.slot.indexOf(slot); if (index > -1) { - modifier.slots.splice(index, 1); - if (modifier.slots.length === 0) { - delete modifier.slots; - } + modifier.slot.splice(index, 1); invalidated(); } } diff --git a/view.js b/view.js index 3206d6ea..a9c9f3c6 100644 --- a/view.js +++ b/view.js @@ -127,7 +127,7 @@ function generateEntry(entry, i, size) { if (entry.children) { for (let j = 0; j < entry.children.length; j += 1) { let $child = generateEntry(entry.children[j], j, entry.children.length); - $child.removeClass('entry').addClass('child'); + $child.attr('data-field', 'children[]'); $entry.children('.card-body').append($child); } } @@ -216,8 +216,8 @@ function generateFunction(func, i) { delete func.treasure; } - if (func.function === 'minecraft:looting_enchant' || func.function === 'minecraft:looting_enchant' || func.function === 'minecraft:limit_count') { - if (func.function === 'minecraft:looting_enchant' || func.function === 'minecraft:limit_count') { + if (func.function === 'minecraft:looting_enchant' || func.function === 'minecraft:limit_count') { + if (func.function === 'minecraft:looting_enchant') { $function.find('.function-limit').removeClass('d-none'); $function.find('.function-limit input').val(func.limit); } else { @@ -317,7 +317,7 @@ function generateFunction(func, i) { if (func.function === 'minecraft:apply_bonus') { $function.find('.function-enchantment').removeClass('d-none'); - $function.find('.function-entity input').val(func.enchantment); + $function.find('.function-enchantment input').val(func.enchantment); } else { delete func.enchantment; } @@ -392,8 +392,8 @@ function generateModifier(modifier, i) { generateRange($modifier.find('.modifier-amount'), modifier.amount); $modifier.find('.modifier-operation').val(modifier.operation); - if (modifier.slots) { - for (let s of modifier.slots) { + if (modifier.slot) { + for (let s of modifier.slot) { let item = $modifier.find('.dropdown-item[data-slot="' + s + '"]'); item.addClass('d-none'); let html = ''; @@ -623,7 +623,7 @@ function generateCondition(condition, i) { if (condition.term) { let $term = generateCondition(condition.term, 0); - $term.removeClass('condition').addClass('term'); + $term.attr('data-field', 'term'); $term.find('.card-header').remove(); $condition.children('.card-body').append($term); } @@ -631,7 +631,7 @@ function generateCondition(condition, i) { if (condition.terms) { for (let j = 0; j < condition.terms.length; j += 1) { let $term = generateCondition(condition.terms[j], j); - $term.removeClass('condition').addClass('terms'); + $term.attr('data-field', 'terms[]'); $condition.children('.card-body').append($term); } } @@ -693,7 +693,7 @@ function generateEntity(entity) { } if (entity.location) { let $location = generateLocation(entity.location); - $location.removeClass('predicate').addClass('location'); + $location.attr('data-field', 'location'); $entity.children('.card-body').append($location); } if (entity.nbt) { @@ -756,7 +756,7 @@ function generateItem(item) { } function generateDamage(damage) { - let $damage = $('#damageTemplate').clone().removeAttr('id').addClass('predicate'); + let $damage = $('#damageTemplate').clone().removeAttr('id'); if (!damage) { damage = {}; } @@ -774,12 +774,12 @@ function generateDamage(damage) { if (damage.source_entity) { let $entity = generateEntity(damage.source_entity); - $entity.removeClass('predicate'); + $entity.attr('data-field', 'source_entity'); $damage.find('.source-entity').append($entity); } if (damage.direct_entity) { let $entity = generateEntity(damage.direct_entity); - $entity.removeClass('predicate'); + $entity.attr('data-field', 'direct_entity'); $damage.find('.direct-entity').append($entity); }