๐Code Examples
Here's a few examples to get started with infinite-craft.
The infinite-craft library's module is named infinitecraft.
You can import the InfiniteCraft class to deal with the game.
from infinitecraft import InfiniteCraftUsing InfiniteCraft
There's multiple ways to use the InfiniteCraft class.
The main way would be using the async with keyword but for larger applications we recommend to use the InfiniteCraft.start() and InfiniteCraft.close() functions to control your session instead.
Intialising the InfiniteCraft class will make a discoveries.json file.
Using async with
async withWe can use async with to automatically start and close our sessions. This is useful for small one file projects.
import asyncio
from infinitecraft import InfiniteCraft
async def main():
async with InfiniteCraft() as game: # automatically start session and end session on async with end
print(f"Pairing elements: {game.discoveries[0]} and {game.discoveries[1]}")
result = await game.pair(game.discoveries[0], game.discoveries[1]) # Pair Water and Fire
print(f"Result: {result}")
asyncio.run(main())Manually controlling
Manually controlling basically means using the start() and stop() functions from InfiniteCraft to manually control the session. This is useful when using bigger multiple file projects.
Using async with while manually controlling
async with while manually controllingUsing async with while manually controlling is possible, but you have to pass in an extra argument manual_control=True so the session does not try to start and close automatically when using async with. This is just personal preference for some people who'd like to use infinite-craft library this way.
Pinging the API
You can use the ping() function from InfiniteCraft to ping the API during a session.
List discovered elements
You can use game.discoveries to list currently discovered elements.
Get a discovery
You can get a discovery using the get_discovery() function.
Fetch all discoveries
You can fetch all discoveries straight from the discoveries.json file using get_discoveries().
Fetch and save
While fetching the discoveries, you can choose to update the game.discoveries attribute as well. Useful for when you make your own changes to the discoveries.json file and want to sync it with the current session.
You can pass set_value=True to achieve this behaviour.
Fetch with a check
While fetching discoveries, you can also choose to use a check to fetch them aswell. Specify the check by using the check=callable argument.
Pairing two elements
You can use the ping() function from InfiniteCraft to pair two elements together.
Pairing automatically saves the newly paired element in game.discoveries.
Pair without saving
You can also pair elements without saving them in game.discoveries by specifying store=False.
Pair undiscovered elements
You can create your own elements by making an Element object and passing it in pair().
Last updated