Coder Social home page Coder Social logo

node-mojangson's Introduction

node-mojangson

NPM version CI

node-mojangson is a mojangson parser.

Mojangson specification

Mojangson is mojang's variant of json. It is basically json with the following changes :

  • array can be indexed (example : [0:"v1",1:"v2",2:"v3"])
  • array and object can have trailing comma (example : [5,4,3,] and {"a":5,"b":6,})
  • there can be string without quote (example : {mykey:myvalue})
  • numbers can be suffixed by b, s, l, f, d, i or the same in upper case (example : {number:5b})
  • mojangson stays a superset of json : every json is a mojangson

Reference https://minecraft.gamepedia.com/Commands#Data_tags

Parser

This parser is build using nearley.

See the grammar and the examples in the test for more information.

Usage

Usage example :

const mojangson = require('mojangson')

const data = mojangson.parse('{mykey:myvalue}')

// print the parsed data
console.log(data)

// print the simplified data
condole.log(mojangson.simplify(data))

The provided method mojangson.parse return a javascript object corresponding to the mojangson passed in input.

mojangson.simplify returns a simplified representation : keep only the value to remove one level. This loses the types so you cannot use the resulting representation to write it back.

mojangson.stringify will take a js object with types and values for mojangson and make it into a normalized mojangson string

const mojangson = require('mojangson')
const data = mojangson.stringify({ type: 'list', value: { type: 'string', value: [ 'z1', 'z2' ] } })
console.log(data) // => [z1,z2]

Another example, the provided method mojangson.normalize takes a string of mojangson and normalizes it in the shortest way to retain all data. Comparing it to the original will tell you if you have the shortest equivalent to a string of mojangson.

const mojangson = require('mojangson')
const original = '[0:"z1",1:"z2"]'
const data = mojangson.normalize(original)
console.log(data) // => [z1,z2]
const optimized = original === data
console.log(optimized) // => false

History

2.0.4

  • fix publish

2.0.3

  • Fix simplifying of arrays throwing error (@Lianecx)
  • Escape new lines in text (@U9G)

2.0.2

  • fixes issue where strings starting with a number would be parsed as a number (@U9G)

2.0.1

  • fix escaping some chars in mojangson.stringify (@U9G)
  • update grammar for unicode escaped strings (@U9G)
  • fix parsing of escaped characters (@Majorblake)

2.0.0

  • Changes output format to include types (@Karang)
  • add simplify function
  • Add stringify function to go back to mojangson (@U9G)

1.1.1

  • Fix empty string parsing (thanks @IdanHo)

1.1.0

  • switch to nearley parser for a better handling for mojangson (thanks @Karang)

1.0.0

  • stop printing error

0.2.4

  • add support for double and int

0.2.3

  • fix release about grammar.js still containing the cli

0.2.2

  • disable jison cli to make mojangson compatible with browserify

0.2.1

  • fix state conflict due to recent trailing comma fix

0.2.0

  • Rename npm package to mojangson
  • fix trailing comma in arrays

0.1.1

  • better error displaying

0.1

  • First release, basic functionality

node-mojangson's People

Contributors

darkflame72 avatar deathcap avatar dependabot-preview[bot] avatar dependabot[bot] avatar idanho avatar joehainess avatar karang avatar lianecx avatar rom1504 avatar u9g avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-mojangson's Issues

Mojangson fails to parse \n in regular text

"§a§oOnly the true hunter can wield
                                   ^

SyntaxError: Error parsing text '{id:"minecraft:diamond_pickaxe",Count:1b,Damage:0s,tag:{display:{Name:"§a§lHeroic Broteas §aDiamond Pickaxe§r§l §a§l369§c§l XI",Lore:[0:"§a§oOnly the true hunter can wield",1:"§a§othese powerful objects used to",2:"§a§ocause the demise and sorrow",3:"",4:"§b§lXP Mastery§7 - §f+20% XP",5:"§b§lHoarder§7 - §f+50% Ores",6:"§b§lGrinder§7 - §f+30% Mining Speed",7:"§b§lShard Mastery§7 - §f2.3x Shard Chance",8:"§b§lInquisitive§7 - §f1.0% Chance to get an extra 10x XP in a bottle",9:"",10:"§dEnergy Hoarder §bV",11:"§d§lEternal Luck §b§lVII",12:"§d§lMeticulous Efficiency §b§lVI",13:"§d§lTime Warp §b§lIV§7 (§f19,184§7 / §f25,000§7)",14:"§6§lCombo Rupture §b§lV",15:"§6§lMomentum §b§lVI",16:"§6§lShatter §b§lVI",17:"§e§lFortify §b§lIII",18:"§e§lFracture §b§lV",19:"§e§lMagnetism §b§lV",20:"§e§lMeteor Hunter §b§lV",21:"§9§lOre Surge §b§lV",22:"§9§lPowerball §b§lIII",23:"§9§lReplenish §b§lV",24:"§9§lSuper Breaker §b§lV",25:"§a§lAlchemy §b§lIII",26:"§a§lTransfuse §b§lIII",27:"§f§lAqua Affinity §b§lI",28:"§f§lFeed §b§lI",29:"§f§lOre Magnet §b§lV",30:"§f§lShard Discoverer §b§lV",31:"§f§lSixth Sense §b§lI",32:"",33:"§f§lWHITESCROLLED",34:"",35:"§a§l§m= §f§l 114§a§l Enchants §a§l§m =",36:"§c§l§m= §f§l 254§c§l Failures §c§l§m =",37:"",38:"§b§lCosmic Energy",39:"§r§a§l§c§l▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏▏ §f§l0%",40:"§7(§f0§7 / 5,308,800)",41:"",42:"§f§l§n3§7/§f10§b Charge Orb §7slots unlocked",43:"",44:"§f11,293,153 §3§lPrismarine Ore",45:"§f17,245,312 §a§lEmerald Ore",46:"§f10,617,350 §b§lDiamond Ore",47:"",48:"§eRequired Mining Level §r§f§l90",49:"",50:"§6§lSkin (§c§lHyperSpace§6§l§6§l)",51:"§7(§nCosmicClient.com§7 required)"]},HideFlags:63,ench:[0:{id:241s,lvl:1s}],_x:"prisonspickaxe",_xu:-6059361399456582300L,_xl:-3671673037138150647L,_xr:18145922L,chargable:{level:369,prestigeCount:11,nickname:"§a§lHeroic Broteas §aDiamond Pickaxe",desc:"§a§oOnly the true hunter can wield
§a§othese powerful objects used to
§a§ocause the demise and sorrow",whiteScroll:1b,enchants:{AQUAAFFINITY:1,FORTUNE:5,GENERATION:5,SIXTH_SENSE:1,FEED:1,LUCK:5,FORTIFY:3,TRANSFUSE:3,EXEC_EFFICIENCY:6,METEOR_HUNTER:5,ORE_SURGE:5,TNT:6,ALCHEMY:3,MOMENTUM:6,POWERBALL:3,POWERTOOL:5,FRACTURE:5,MAGNETISM:5,COMBO_RUPTURE:5,EXEC_LUCKY:7,EXEC_WARPMINER:4,EXEC_ENERGY:5},enchantCooldowns:{WARPMINER:231L,FORTIFY:1660921694161L,MOMENTUM:1660922444134L,POWERBALL:1660919050862L},enchantData:{WARPMINER:19184L,FORTIFY:1660919141549L,MOMENTUM:1660913877398L},prestiges2:{GRINDER:30,HOARDER:50,XP_MASTERY:20,SHARD_MASTERY:23,INQUISITIVE:10},skins:[0:"PRISONS_SPEEDY_PICKAXE"],skinData:{PRISONS_SPEEDY_PICKAXE:1L},blocks:[576448,53609,45363,11417,504485,10617350,17245312,11293153,0,]},cosmicData:{itemSkinType:"PRISONS_SPEEDY_PICKAXE"}}}'
    at JSON.parse (<anonymous>)
    at Rule.postprocess (C:\Users\-\Documents\git-proj\proxy\node_modules\mojangson\grammar.js:127:129)
    at State.finish (C:\Users\-\Documents\git-proj\proxy\node_modules\nearley\lib\nearley.js:68:35)
    at Column.process (C:\Users\-\Documents\git-proj\proxy\node_modules\nearley\lib\nearley.js:92:23)
    at Parser.feed (C:\Users\-\Documents\git-proj\proxy\node_modules\nearley\lib\nearley.js:338:24)
    at Object.parse (C:\Users\-\Documents\git-proj\proxy\node_modules\mojangson\index.js:93:14)
    at remakeItemsInComponent (C:\Users\-\Documents\git-proj\proxy\index.js:89:72)
    at remakeItemsInComponent (C:\Users\-\Documents\git-proj\proxy\index.js:98:5)
    at handleChat (C:\Users\-\Documents\git-proj\proxy\index.js:82:5)
    at InstantConnectProxy.<anonymous> (C:\Users\-\Documents\git-proj\proxy\index.js:71:20)

mojangson vs node-mojangson

The readme example uses the module name "mojangson"

var mojangson=require("mojangson");

but it is published as "node-mojangson":

https://www.npmjs.com/package/node-mojangson

Could be reconciled by updating the readme example to use node-mojang or republishing as mojangson. I'd favor the latter, https://www.npmjs.com/package/mojangson is available and NPM recommends against node- prefix naming in modules because it is implied: (note that the GitHub repository could still stay named as node-mojangson, since the context is less clear)

https://www.npmjs.com/package/node-mojangson

  • Don't put "js" or "node" in the name. It's assumed that it's js, since you're writing a package.json file, and you can specify the engine using the "engines" field. (See below.)

Issue Parsing Items

The Error:

Error parsing text '{id:299,Damage:0,tag:{display:{Name:§f§d§l\§f§lSayMums§d§l\,Lore:[§7Protection IV,§7Projectile Protection II,§7Unbreaking III,§dMega Heavy V,§dInfinite Luck V,§dGhostly Ghost III,§dSoul Hardened III,§dGodly Overload III,§dDivine Enlighted III,§dPaladin Armored IV,§dReinforced Tank IV,§dVengeful Diminish VI,§dHeroic Enchant Reflect IX,§dPlanetary Deathbringer III,§eMarksman IV,§eAngelic V,§eValor V,§8A firey shroud that echoes absolute,§8power and domination. No mere human,§8would dare stand against its owner., ,§8§lKit Level| §c10 / 10,§0IID 109604253,§a§l14 Enchantment Slots§7 (Orb [§a+5§7]), ,§7+8 Armor Value,§71000 Durability,§4This armor is stronger than diamond.,§6§lArmor Crystal (§4§lSupreme§7 §c§lPhantom§7 §6§lEngineer§7 §5§lDimensional Traveler§7§6§l),§6§lAnti M-Kit Crystal (§2§lNecromancer§7 §9§lDeath Knight§7§6§l), ,§3§l+ AMULET [§8N/A§3§l],§6§l~§6ShooterMatty,§c§lCORRUPT (§7cannot be protected§c§l),§f§lPROTECTED]}}}'
{"extra":[{"color":"white","text":"\u003cInterstellar\u003e XQuakQuak: "},{"color":"white","hoverEvent":{"action":"show_item","value":"{id:299,Damage:0,tag:{display:{Name:\\SayMums\\,Lore:[Protection IV,Projectile Protection II,Unbreaking III,Mega Heavy V,Infinite Luck V,Ghostly Ghost III,Soul Hardened III,Godly Overload III,Divine Enlighted III,Paladin Armored IV,Reinforced Tank IV,Vengeful Diminish VI,Heroic Enchant Reflect IX,Planetary Deathbringer III,Marksman IV,Angelic V,Valor V,A firey shroud that echoes absolute,power and domination. No mere human,would dare stand against its owner., ,Kit Level| 10 / 10,IID 109604253,14 Enchantment Slots (Orb [+5]), ,+8 Armor Value,1000 Durability,This armor is stronger than diamond.,Armor Crystal (Supreme Phantom Engineer Dimensional Traveler),Anti M-Kit Crystal (Necromancer Death Knight), ,+ AMULET [N/A],~ShooterMatty,CORRUPT (cannot be protected),PROTECTED]}}}"},"text":"» \"SayMums\" (x1) «"},{"color":"white","text":" in ah"}],"text":""}

My code:

function listeners(bot){
    bot.on('message', (jsonMsg) => {
        console.log(jsonMsg.toString())
    })
}

Example of an Item: https://gyazo.com/8f9aecbd15201d2896dc37e1af01ddda

I was told me make an issue on here from the discord. My console is spammed with these error messages while running the bot but it does not cause my code to stop running

Parsing empty list and simplifying parse result throws error

Recreation:

const mojangson = require("mojangson");
const empty_array_parse = mojangson.parse("{EmptyArray: []}");
console.log(mojangson.simplify(empty_array_parse));

Output:

Uncaught TypeError: Cannot read properties of undefined (reading 'map')
    at transform (/home/node_modules/mojangson/index.js:13:26)
    at simplify (/home/node_modules/mojangson/index.js:17:10)
    at /home/node_modules/mojangson/index.js:8:20
    at Array.reduce (<anonymous>)
    at transform (/home/node_modules/mojangson/index.js:7:33)
    at Object.simplify (/home/node_modules/mojangson/index.js:17:10)

I think it has something to do with there being no checks to verify the "value" key exists on line 13 of index.js. Adding if (value.value == undefined) return [] to index.js fixes it in this case but there might be exceptions.

Parse error when parsing trailing comma array example

The readme says this mojangson is valid:

array and object can have trailing comma (example : [5,4,3,] and {"a":5,"b":6,})

but it fails to parse:

> mojangson.parse('[5,4,3,]')
Error parsing text '[5,4,3,]'
Error: Parse error on line 1:
[5,4,3,]
-------^
Expecting 'LEFTC', 'POSITIVE_INTEGER', 'STRING', 'STRINGWITHOUTQUOTE', 'TRUE', 'FALSE', 'NULL', 'FLOAT', 'FLOATSUFFIX', 'LEFTP', got 'RIGHTC'
    at Object.parseError (node-mojangson/grammar.js:148:15)
    at Object.parse (grammar.js:213:22)
    at Object.module.exports.parse (/index.js:5:19)
    at repl:1:11
    at REPLServer.defaultEval (repl.js:248:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:412:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)

the trailing comma object ({"a":5,"b":6,}) example parses ok

bug in new mojangson implementation

Hey, it seems to not be able to parse the following text:

Error: Error parsing text '{id:"minecraft:yellow_shulker_box",Count:1b,tag:{BlockEntityTag:{CustomName:"Stacked Totems",x:0,y:0,z:0,id:"minecraft:shulker_box",Lock:""},display:{Name:"Stacked Totems"}},Damage:0s}'
offset: 138,
token: { value: '"' }

looks to me like its failing on the empty string.

Incorrect escape string serialization

const origin = '"'
const str = JSON.stringify(origin)
const str2 = MojangSON.parse(MojangSON.stringify({ str })).str

console.log(str === str2) // false
console.log(origin === JSON.parse(str)) // true
console.log(str) // "\""
console.log(str2) // """

And this works:

const escapeJSON = text => text.replace(/"/g, '\\u0022').replace(/'/g, '\\u0027')

const str = JSON.stringify(escapeJSON(origin))
const str2 = MojangSON.parse(MojangSON.stringify({ str })).str

Parser error when connected to Spigot server

This popped up adn crashed my bot:

C:\Users\Jakub\node_modules\node-mojangson\grammar.js:148
        throw new Error(str);
              ^
Error: Parse error on line 1:
...st:31,display:{Name:"/\ Swacker Broad he
-----------------------^
Expecting 'EOF', 'RIGHTC', 'DP', 'COMMA', 'RIGHTP', got 'STRINGWITHOUTQUOTE'
    at Object.parseError (C:\Users\Jakub\node_modules\node-mojangson\grammar.js:148:15)
    at Object.parse (C:\Users\Jakub\node_modules\node-mojangson\grammar.js:213:22)
    at Object.module.exports.parse (C:\Users\Jakub\node_modules\node-mojangson\index.js:3:52)
    at ChatMessage.parse (C:\Users\Jakub\node_modules\mineflayer\lib\chat_message.js:134:41)
    at new ChatMessage (C:\Users\Jakub\node_modules\mineflayer\lib\chat_message.js:17:8)
    at C:\Users\Jakub\node_modules\mineflayer\lib\chat_message.js:44:32
    at Array.forEach (native)
    at ChatMessage.parse (C:\Users\Jakub\node_modules\mineflayer\lib\chat_message.js:39:17)
    at new ChatMessage (C:\Users\Jakub\node_modules\mineflayer\lib\chat_message.js:17:8)
    at parseJsonMessage7 (C:\Users\Jakub\node_modules\mineflayer\lib\plugins\chat.js:74:25)

I suppose that either you're something missing or the server is sending invalid data. If the former is true it remains mystery why normal clients do not have any problems. The error was probably caused by a death message which contained weapon info.

I cannot reproduce this since it wasn't my server.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.