This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/minecraft_items.py

99 lines
2.4 KiB
Python
Raw Normal View History

2012-03-16 04:20:13 +01:00
""" plugin by _303 (?)
"""
from util import hook
import re
pattern = re.compile(r'^(?P<count>\d+)x (?P<name>.+?): (?P<ingredients>.*)$')
recipelist = []
2012-03-23 01:00:22 +01:00
2012-03-16 04:20:13 +01:00
class Recipe(object):
__slots__ = 'output', 'count', 'ingredients', 'line'
def __init__(self, output, count, ingredients, line):
self.output = output
self.count = count
self.ingredients = ingredients
self.line = line
def __str__(self):
return self.line
2012-03-23 01:00:22 +01:00
2012-03-26 06:17:19 +02:00
with open("plugins/data/recipes.txt") as f:
2012-03-16 04:20:13 +01:00
for line in f.readlines():
2012-03-20 03:53:33 +01:00
if line.startswith("//"):
continue
2012-03-16 04:20:13 +01:00
line = line.strip()
match = pattern.match(line)
if not match:
continue
recipelist.append(Recipe(line=line,
output=match.group("name").lower(),
ingredients=match.group("ingredients"),
count=match.group("count")))
2012-03-16 04:20:13 +01:00
ids = []
2012-03-26 06:17:19 +02:00
with open("plugins/data/itemids.txt") as f:
2012-03-16 04:20:13 +01:00
for line in f.readlines():
2012-03-20 03:53:33 +01:00
if line.startswith("//"):
continue
2012-03-16 04:20:13 +01:00
parts = line.strip().split()
id = parts[0]
name = " ".join(parts[1:])
2012-03-23 01:00:22 +01:00
ids.append((id, name))
2012-03-16 04:20:13 +01:00
2012-05-16 05:08:26 +02:00
@hook.command("mcid")
2012-03-16 04:20:13 +01:00
@hook.command
2012-05-16 05:08:26 +02:00
def mcitem(input, reply=None):
"""mcitem <item/id> -- gets the id from an item or vice versa"""
2012-03-16 04:20:13 +01:00
input = input.lower().strip()
if input == "":
2012-03-19 04:07:10 +01:00
reply("error: no input.")
2012-03-16 04:20:13 +01:00
return
results = []
for id, name in ids:
2012-03-20 04:10:25 +01:00
if input == id:
2013-09-05 04:11:18 +02:00
results = ["\x02[{}]\x02 {}".format(id, name)]
2012-03-20 04:10:25 +01:00
break
elif input in name.lower():
2013-09-05 04:11:18 +02:00
results.append("\x02[{}]\x02 {}".format(id, name))
2012-03-16 04:20:13 +01:00
2012-05-16 21:47:49 +02:00
if not results:
return "No matches found."
2012-03-23 01:00:22 +01:00
2012-03-20 04:10:25 +01:00
if len(results) > 12:
2013-09-05 04:11:18 +02:00
reply("There are too many options, please narrow your search. ({})".format(str(len(results))))
2012-03-16 04:20:13 +01:00
return
2012-03-23 01:00:22 +01:00
2012-03-19 03:57:43 +01:00
out = ", ".join(results)
2012-03-23 01:00:22 +01:00
2012-03-19 03:57:43 +01:00
return out
2012-03-16 04:20:13 +01:00
2012-05-16 05:08:26 +02:00
@hook.command("mccraft")
2012-03-16 04:20:13 +01:00
@hook.command
2012-05-16 05:08:26 +02:00
def mcrecipe(input, reply=None):
"""mcrecipe <item> -- gets the crafting recipe for an item"""
2012-03-16 04:20:13 +01:00
input = input.lower().strip()
2012-05-16 21:47:49 +02:00
results = [recipe.line for recipe in recipelist
if input in recipe.output]
2012-03-16 04:20:13 +01:00
2012-05-16 21:47:49 +02:00
if not results:
return "No matches found."
2012-03-19 03:57:43 +01:00
if len(results) > 3:
2013-09-05 04:11:18 +02:00
reply("There are too many options, please narrow your search. ({})".format(len(results)))
2012-03-16 04:20:13 +01:00
return
for result in results:
reply(result)