mindstorm38 / portablemc

A fast, reliable and cross-platform command-line Minecraft launcher and API for developers. Including fast and easy installation of common mod loaders such as Fabric, Forge, NeoForge and Quilt.
https://pypi.org/project/portablemc/
GNU General Public License v3.0
320 stars 19 forks source link

Installing fabric and mods on it #183

Closed ZeralldMC closed 8 months ago

ZeralldMC commented 8 months ago

Hi. My question may seem stupid or somehow simple, but I can't run fabric. I have installed portablemc-fabric and I am trying to run it like this:

@eel.expose
def startGame():
    with open('app/config.json', 'r') as f:
        appConfig = json.load(f)

    if(appConfig["game"]["version"]):
        gameVersion = appConfig["game"]["version"]
    else:
        gameVersion = "1.20.2"

    responseForIp = requests.get('https://mcvs.yurba.one/api/servers.php')
    servers = responseForIp.json()

    main_dir = "app/user/data"
    work_dir = "app/user/minecraft"
    game_width = appConfig["game"]["width"]
    game_height = appConfig["game"]["height"]

    manifest = VersionManifest()
    ctx = Context(main_dir, work_dir)

    # Here we use the fabric: prefix to specify the fabric version
    # We also add the fabric loader version after the game version
    version_id, alias = manifest.filter_latest(f"fabric:{gameVersion}")
    version = Version(ctx, version_id)

    try:
        version.install(jvm=True)

        start_opts = StartOptions()
        start_opts.disable_multiplayer = False
        start_opts.server_address = servers["main"]["ip"]
        start_opts.username = appConfig["accounts"]["currentPlayer"]
        start_opts.resolution = (game_width, game_height)

        start = Start(version)
        start.prepare(start_opts)
        start.start()

    except VersionError as e:
        print(f"Version '{e.version}' not found.")
    except JvmLoadingError as e:
        print(f"No JVM found for you system.")

    eel.game_loader_end()

I tried asking neural network but it was of little use 😋

My goal is to run the latest version of fabric and immediately install there mod - Sodium for better optimization of the game. Can you please tell me what I'm doing wrong? I will be grateful

mindstorm38 commented 8 months ago

It looks like you're using an old version of the API which doesn't support fabric by default. Could you try with latest version >= 4.0.0. Latest version have fabric support by default, the API documentation also have examples how to use it.

ZeralldMC commented 8 months ago

It looks like you're using an old version of the API which doesn't support fabric by default. Could you try with latest version >= 4.0.0. Latest version have fabric support by default, the API documentation also have examples how to use it.

I updated portablemc to version 4.0.3, but most likely that's not the issue. I forgot to mention that when I run this code, the console shows something like:

Version 'fabric:1.20.2' not found.

I'm afraid I've done something wrong

mindstorm38 commented 8 months ago

With the newer API you don't have to use fabric:XXX like identifier, you can (and should) use the dedicated class, as explained here: https://github.com/mindstorm38/portablemc/blob/master/doc/API.md#fabricquilt-version

ZeralldMC commented 8 months ago

With the newer API you don't have to use fabric:XXX like identifier, you can (and should) use the dedicated class, as explained here: https://github.com/mindstorm38/portablemc/blob/master/doc/API.md#fabricquilt-version

I have a problem installing portablemc.fabric

I don't understand how it works, but when I install portablemc.fabric via pip, it doesn't want to install as:

from portablemc.fabric import FabricVersion

Only as portablemc-fabric, but then the imported FabricVersion does not have the with_fabric() method, as python tells me on startup. Here is the code:

from portablemc import StartOptions
from portablemc import Start
from portablemc import Context
from portablemc import VersionManifest
from portablemc import Version, VersionError, JvmLoadingError
from portablemc_fabric import FabricVersion

@eel.expose
def startGame():
    With open('app/config.json', 'r') as f:
        appConfig = json.load(f)

    If(appConfig["game"]["version"]):
        gameVersion = appConfig["game"]["version"]
    else:
        gameVersion = "1.20.2"

    responseForIp = requests.get('https://mcvs.yurba.one/api/servers.php')
    servers = responseForIp.json()

    main_dir = "app/user/data"
    work_dir = "app/user/minecraft"
    game_width = appConfig["game"]["width"]
    game_height = appConfig["game"]["height"]

    manifest = VersionManifest()
    ctx = Context(main_dir, work_dir)

    # Here we use the fabric: prefix to specify the fabric version
    # We also add the fabric loader version after the game version
    version_id, alias = manifest.filter_latest(f "fabric:{gameVersion}")
    version = FabricVersion.with_fabric("1.20.2")

    try:
        version.install(jvm=True)

        start_opts = StartOptions()
        start_opts.disable_multiplayer = False
        start_opts.server_address = servers["main"]["ip"]
        start_opts.username = appConfig["accounts"]["currentPlayer"]
        start_opts.resolution = (game_width, game_height)

        start = Start(version)
        start.prepare(start_opts)
        start.start()

    except VersionError as e:
        print(f "Version '{e.version}' not found.")
    except JvmLoadingError as e:
        print(f "No JVM found for you system.")

    eel.game_loader_end()
mindstorm38 commented 8 months ago

The different add-on packages portablemc-fabric, portablemc-forge, are all deprecated and no longer works with the newer launcher v4 API. You can uninstall them (it may be required to update portablemc to v4), and then ensure that portablemc is up-to-date (with pip install --upgrade portablemc), then you should be able to import: from portablemc.fabric import FabricVersion.

mindstorm38 commented 8 months ago

The API has changed a lot, so your current code may need changes. However, the upgrade is worth the cost because the code is much cleaner and reliable.

ZeralldMC commented 8 months ago

Thanks, I figured it out, but the documentation is still a bit unclear to me.

I'm able to run minecraft, but I want to specify the resolution for the window, and I know that the "resolution" argument is responsible for that, but how do I specify it? resolution=[1280, 720]? Or how to specify a user's nickname offline?

mindstorm38 commented 8 months ago

Once you have constructed your version object with something like this: version = FabricVersion.with_fabric(). You can set the resolution with version.resolution = (1280, 720), you can also set the offline nickname with version.set_auth_offline("OfflineNick") (you can even set the UUID if you wish). The options are listed here: https://github.com/mindstorm38/portablemc/blob/master/doc/API.md#other-options

ZeralldMC commented 8 months ago

Thank you for help!