From d8ce84e385170ae5a16cc97614df89b3d8683d7a Mon Sep 17 00:00:00 2001 From: Misode Date: Fri, 21 Jun 2019 16:46:56 +0200 Subject: [PATCH] Add damage object and improve NBT fields --- custom.css | 8 ++++ index.html | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++-- model.js | 68 ++++++++++++++++++++++++++- view.js | 76 +++++++++++++++++++++++++++--- 4 files changed, 272 insertions(+), 12 deletions(-) diff --git a/custom.css b/custom.css index 70fe2bd4..294eeb6f 100644 --- a/custom.css +++ b/custom.css @@ -28,6 +28,10 @@ } .card.bg-dark .card.bg-dark { + background-color: #565b60 !important; +} + +.card.bg-dark .card.bg-dark .card.bg-dark { background-color: #4b4f54 !important; } @@ -43,6 +47,10 @@ -webkit-tab-size: 4; } +textarea { + min-height: 37.6px; +} + textarea.invalid { border-color: red !important; } diff --git a/index.html b/index.html index 4dbbc9d0..0df520ef 100644 --- a/index.html +++ b/index.html @@ -10,7 +10,7 @@
@@ -226,7 +226,7 @@
Tag
- +
@@ -329,7 +329,7 @@
Name
- +
@@ -717,7 +717,7 @@
NBT
- +
@@ -778,7 +778,129 @@
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 7090ec80..0f2d4790 100644 --- a/model.js +++ b/model.js @@ -6,7 +6,7 @@ $('#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'; +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: [] @@ -95,6 +95,10 @@ function getParent(el) { 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; } } @@ -245,7 +249,19 @@ function deleteValue(root, field) { 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) { + console.log('update', field, '->', $(el).val()); + console.log(getParent(el)); updateValue(getParent(el), field, $(el).val()); invalidated(); } @@ -313,6 +329,26 @@ function getRangeField($el, type) { } } +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); + } + } + invalidated(); +} + function addEnchantment(el) { let func = getParent(el); let enchantment = $(el).attr('data-ench'); @@ -527,3 +563,33 @@ function updateItemField(el, type) { } invalidated(); } + +function toggleDamageFlags(el) { + let parent = getParent(el); + if (parent.type) { + delete parent.type; + } else { + parent.type = {}; + } + invalidated(); +} + +function toggleSourceEntity(el) { + let parent = getParent(el); + if (parent.source_entity) { + delete parent.source_entity; + } else { + parent.source_entity = {}; + } + invalidated(); +} + +function toggleDirectEntity(el) { + let parent = getParent(el); + if (parent.direct_entity) { + delete parent.direct_entity; + } else { + parent.direct_entity = {}; + } + invalidated(); +} diff --git a/view.js b/view.js index d3040005..b4e720b1 100644 --- a/view.js +++ b/view.js @@ -4,6 +4,13 @@ function invalidated() { $('#source').val(JSON.stringify(table, null, indentation)); } +function preventNewline(e) { + if (e.which === 13) { + $(e.target).trigger('change'); + e.preventDefault(); + } +} + function generateRange($el, data) { if (typeof data === 'object') { if (data.type && data.type.match(/(minecraft:)?binomial/)) { @@ -24,6 +31,14 @@ function generateRange($el, data) { } } +function generateRadio($el, data) { + if (data === true) { + $el.find('[value="true"]').addClass('active'); + } else if (data === false) { + $el.find('[value="false"]').addClass('active'); + } +} + function generateStructure() { $('#structure').html(''); @@ -167,7 +182,7 @@ function generateFunction(func, i) { } } $function.find('.function-nbt').removeClass('d-none'); - $function.find('.function-nbt input').val(func.tag); + $function.find('.function-nbt textarea').val(func.tag).keydown(e => preventNewline(e)); } else { delete func.tag; } @@ -234,7 +249,7 @@ function generateFunction(func, i) { if (typeof value !== 'string') { value = JSON.stringify(value); } - $function.find('.function-name input').val(value); + $function.find('.function-name input').val(value).keydown(e => preventNewline(e)); } else { delete func.name; } @@ -485,7 +500,7 @@ function generateCondition(condition, i) { delete condition.properties; } - if (condition.condition === 'minecraft:entity_properties' || condition.condition === 'minecraft:location_check' || condition.condition === 'minecraft:match_tool') { + if (condition.condition === 'minecraft:entity_properties' || condition.condition === 'minecraft:location_check' || condition.condition === 'minecraft:match_tool' || condition.condition === 'minecraft:damage_source_properties') { if(!condition.predicate) { condition.predicate = {}; @@ -514,10 +529,17 @@ function generateCondition(condition, i) { } } + if (condition.condition === 'minecraft:damage_source_properties') { + let $damage = generateDamage(condition.predicate); + $condition.children('.card-body').append($damage); + if (condition.predicate) { + delete condition.nbt; + } + } + if (condition.condition === 'minecraft:match_tool') { let $item = generateItem(condition.predicate); $condition.children('.card-body').append($item); - } else { } } else { @@ -684,7 +706,7 @@ function generateEntity(entity) { } } $entity.find('.type').val(entity.type); - $entity.find('.nbt').val(entity.nbt); + $entity.find('.nbt').val(entity.nbt).keydown(e => preventNewline(e)); if (entity.type === '') { delete entity.type; } @@ -716,7 +738,7 @@ function generateItem(item) { } generateRange($item.find('.item-count'), item.count); generateRange($item.find('.item-durability'), item.durability); - $item.find('.nbt').val(item.nbt); + $item.find('.nbt').val(item.nbt).keydown(e => preventNewline(e)); $item.find('.potion').val(item.potion); if (item.name === '') { delete item.name; @@ -733,3 +755,45 @@ function generateItem(item) { return $item; } + +function generateDamage(damage) { + let $damage = $('#damageTemplate').clone().removeAttr('id').addClass('predicate'); + if (!damage) { + damage = {}; + } + if (damage.type) { + $damage.find('.damage-flag').removeClass('d-none'); + generateRadio($damage.find('.damage-projectile'), damage.type.is_projectile); + generateRadio($damage.find('.damage-explosion'), damage.type.is_explosion); + generateRadio($damage.find('.damage-fire'), damage.type.is_fire); + generateRadio($damage.find('.damage-magic'), damage.type.is_magic); + generateRadio($damage.find('.damage-lightning'), damage.is_lightning); + generateRadio($damage.find('.damage-starvation'), damage.type.bypasses_magic); + generateRadio($damage.find('.damage-void'), damage.type.bypasses_invulnerability); + generateRadio($damage.find('.damage-armor'), damage.type.bypasses_armor); + } + + if (damage.source_entity) { + let $entity = generateEntity(damage.source_entity); + $entity.removeClass('predicate'); + $damage.find('.source-entity').append($entity); + } + if (damage.direct_entity) { + let $entity = generateEntity(damage.direct_entity); + $entity.removeClass('predicate'); + $damage.find('.direct-entity').append($entity); + } + + if (typeof damage.dealt !== 'object' && isNaN(damage.dealt)) { + delete damage.dealt; + } + if (typeof damage.dealt !== 'object' && isNaN(damage.taken)) { + delete damage.taken; + } + generateRange($damage.find('.damage-dealt'), damage.dealt); + generateRange($damage.find('.damage-taken'), damage.taken); + + generateRadio($damage.find('.damage-blocked'), damage.blocked); + + return $damage; +}