AsherGlick / ResourceCalculator

A Video Game Resource Calculator
https://resourcecalculator.com
GNU General Public License v3.0
57 stars 32 forks source link

Import Litematica Schematic Material List #42

Closed mmodrow closed 2 years ago

mmodrow commented 2 years ago

Hi Asher,

I am implementing a parser to import litematica schematic material files into the resource calculator (at least for the MC calculator). That's so I can easily track my needs for bigger projects, that I plan ahead in creative.

Would you be interested in a proper integration? Otherwise I'd just tampermonkey it in for my own benefit.

Here is my POC JS-snippet for it:

function materialListToRequestHash(inputString){
  inputString = inputString.replace(/(\r?\n|^)\+-+\+-+\+-+\+-+\+(\r?\n|$)/gm, "$1");
  inputString = inputString.replace(/(\r?\n|^)\|[^|]+\|(\r?\n|$)/gm, "$1");
  inputString = inputString.replace(/(\r?\n|^)\|[^0-9]+\|(\r?\n|$)/gm, "$1");
  inputString = inputString.replace(/\| +(.+?) +\| +(\d+) +\| +(\d+) +\| +(\d+) +\|/gm, "$1;$2;$3;$4");
  inputString = inputString.replace(/\r\n$/gm, "\n");
  inputString = inputString.trim().toLowerCase().replaceAll(" ", "");
  var items = inputString.split("\n");
  var totals = items.map((item) => {
    var itemTotal = item.split(";");
    return [itemTotal[0],itemTotal[1]].join("=");
  });
  var totalsQuery = "#" + totals.join("&");
  return totalsQuery;  
}

var element = document.createElement('div');
element.innerHTML = '<input type="file">';
var fileInput = element.firstChild;

fileInput.addEventListener('change', function() {
    var file = fileInput.files[0];

    if (file.name.match(/\.(txt)$/)) {
        var reader = new FileReader();

        reader.onload = function() {
            window.location.hash = materialListToRequestHash(reader.result);
        };

        reader.readAsText(file);    
    } else {
        alert("File not supported, .txt files only");
    }
});

document.body.appendChild(fileInput);

Example material list:


+----------------------+-------+---------+---------------------------------+
| Material List for schematic 'Tunnel Bore (Double Line)' (3 of 3 regions) |
+----------------------+-------+---------+---------------------------------+
| Item                 | Total | Missing |                       Available |
+----------------------+-------+---------+---------------------------------+
| Honey Block          |    12 |      12 |                               0 |
| Observer             |     9 |       9 |                               0 |
| Ancient Debris       |     6 |       6 |                               0 |
| Smooth Stone Slab    |     6 |       6 |                               0 |
| Piston               |     5 |       5 |                               0 |
| Slime Block          |     5 |       5 |                               0 |
| Sticky Piston        |     4 |       4 |                               0 |
| Smooth Stone         |     3 |       3 |                               0 |
| Dead Brain Coral Fan |     2 |       2 |                               0 |
| Detector Rail        |     2 |       2 |                               0 |
| Glowstone            |     2 |       2 |                               0 |
| Stone Brick Wall     |     2 |       2 |                               0 |
| TNT                  |     2 |       2 |                               0 |
| Birch Fence          |     1 |       1 |                               0 |
| Note Block           |     1 |       1 |                               0 |
| Target               |     1 |       1 |                               0 |
+----------------------+-------+---------+---------------------------------+
| Item                 | Total | Missing |                       Available |
+----------------------+-------+---------+---------------------------------+
AsherGlick commented 2 years ago

I had previously given a lot of thought to how to support custom plugins like this in resource calculator. I used to have an NBT structure block parser in the long long ago but it was removed when the site became more generic then just minecraft.

The idea I had for eventually re-adding it is to make a new page where you could load the data in, and have that page redirect to a calculator page where the data was populated into the URL. If you want to follow that design pattern I would be on board with having this added to the main site. Out of the way for people who don't need it while still accessible for those who do. Future thought could be given as to how to make the page discoverable.

I think the fastest method to achieving this is by making a new optional folder called something like "plugins" in the resource folder, and have build.py copy that entire folder into the build directory. Then you can navigate to something like http://resourcecalculator.com/minecraft/plugins/litematica.html to get to this new page.

Does that sound like it would fulfill your use-case?

mmodrow commented 2 years ago

Thanks for giving it so much thought, appreciate it! That would actually work pretty nice. A custom resource, that's accessible to those in the know and where one can be directed to, but that does not clutter the main site sounds very smart and useful. A plug-in directory sounds very sensible as well, as it will be game-specific source code beyond the configuration data. I really like the idea and I guess I will take a shot at that, as soon as I have some spare time for it.

mmodrow commented 2 years ago

@AsherGlick I created a draft for a PR. Obviously this needs some beautification, still, but I'd like to know if this feels acceptable to you from a code & usage perspective.

AsherGlick commented 2 years ago

This seems fine so far. I am not going to be much of a stickler for the code quality of the plugin itself at this point. It can't break anything around it so better to be fast and have a working feature to iterate on then to try and make the best thing right off the bat.

In the future if plugins grow we likely want to do some independent plugin stale-file checking instead of just copying over all the files. If we end up with several plugins for a calculator we will end up copying possibly large files each time we want to rebuild. There should be some helper functions already such as get_newest_modified_time() and get_oldest_modified_time() to take care of most of the heavy lifting. This way we wont have to rebuild the calculator ever time a plugin changes either. Your plugin in small and it is not a problem now so only add this if you want to. If you are feeling really adventurous a --no-plugins flag could be added, and set with --draft to eliminate any time costs associated with having plugins for a calculator. Again this is not necessary for #44 .

mmodrow commented 2 years ago

I will try to give your suggestions a shot. Don't expect too much anyway, as I am not much of a python guy.

"She'll fly but it ain't pretty."

AsherGlick commented 2 years ago

Wish you the best of luck, but if it turns out to be too much for the time you can allot to it the code you have written to copy plugins will be fine.

mmodrow commented 2 years ago

PR ist Ready for Review.

AsherGlick commented 2 years ago

PR accepted