Skip to content

Commit e9bd812

Browse files
authored
Merge pull request #3 from starturtle/feature/count-simplifier
"How Many Do I Need" script
2 parents 9ccffff + 586362b commit e9bd812

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,24 @@ The script is used to compose complex JQL queries from a JSON-encoded dictionary
5151
It will enclose the expansion of sub-queries in parentheses while expanding, in order to ensure the original precedence.
5252

5353
In order to use one query inside another, use the sub-query's name in brackets inside the outer query. See sample.json for an example.
54+
55+
## How Many Do I Need?
56+
The minecraft/hmdin script is basically a counting formatter that can help you keep track of large item quantities in Minecraft.
57+
By default, it will print a human-readable listing of all nonzero "units of quantity" necessary to amass the specified amount of items.
58+
59+
You can specify a nonstandard stack size (e.g. 16 for eggs or similar) using `--stack-size`.
60+
61+
python hmdin.py 256 --stack_size=16
62+
> 256 items are 16 stacks
63+
64+
python hmdin.py 256
65+
> 255 items are 3 stacks and 63 items
66+
67+
python hmdin.py 2000
68+
> 2000 items are 1 chest, 4 stacks and 16 items
69+
70+
The actual helper/split functions are:
71+
72+
hmdin(count, stack_size=64, container_size=27) # will return the tuple of <chests>, <stacks>, <items>
73+
pretty_print(chests, stacks, items) # will format the list of items as human readable (without prefix), e.g. "3 chests and 14 items"
74+
csv_print(chests, stacks, items, separator=",") #will format the list of all three items for CSV output, e.g. comma-separated

minecraft/hmdin.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import argparse
2+
import sys
3+
4+
def hmdin(count, stack_size=64, container_size=27):
5+
i = count % stack_size
6+
s_raw = count // stack_size
7+
s = s_raw % container_size
8+
c = s_raw // container_size
9+
return c, s, i
10+
11+
def pretty_print_kind(count, name):
12+
return f"{count} {name}{'' if count == 1 else 's'}"
13+
14+
def pretty_print(chests, stacks, items):
15+
components = []
16+
if chests > 0:
17+
components.append(pretty_print_kind(chests, "chest"))
18+
if stacks > 0:
19+
components.append(pretty_print_kind(stacks, "stack"))
20+
if items > 0 or len(components) == 0:
21+
components.append(pretty_print_kind(items, "item"))
22+
if len(components) == 1:
23+
return components[0]
24+
return ", ".join(components[:-1]) + " and " + components[-1]
25+
26+
def csv_print(chests, stacks, items, separator=","):
27+
return separator.join(chests, stacks, items)
28+
29+
if __name__ == '__main__':
30+
parser = argparse.ArgumentParser()
31+
parser.add_argument("count", help="Number of items to evaluate", type=int)
32+
parser.add_argument("--stack_size", help="Stack size of the respective item", type=int, default=64)
33+
parser.add_argument("--as_csv", action="store_true")
34+
args = parser.parse_args()
35+
printer = csv_print if args.as_csv else pretty_print
36+
if args.count < 0:
37+
print(f"-{pretty_print_kind(-args.count, 'item')} {'are' if args.count != 1 else 'is'} imaginary.")
38+
sys.exit(1)
39+
print(f"{pretty_print_kind(args.count, 'item')} {'are' if args.count != 1 else 'is'} {printer(*hmdin(args.count, args.stack_size))}.")
40+

0 commit comments

Comments
 (0)