Code Monkey home page Code Monkey logo

dart_minecraft's Introduction

dart_minecraft

Pub Package GitHub Issues GitHub Stars GitHub License

A simple Dart library for interfacing with the Mojang and Minecraft APIs. It also includes NBT read/write functionality and functions to ping Minecraft: Java Edition servers.

You can simply import the library like this:

import 'package:dart_minecraft/dart_minecraft.dart';

Examples

Below are some basic examples of the features included in this library. A better and more extensive example can be found here. However you should always keep in mind that there is a rate limit on all API, set at 600 requests per 10 minutes. You are expected to cache the results and this is not done by the library itself.

Authenticating with Mojang API

dart_minecraft offers functionality to authenticate with the Mojang API called Yggdrasil.

void main() async {
    // This authenticates, invalidates older tokens, and gets a new access
    // and client token. Be sure to store them, but keep them safe.
    MojangAccount account = authenticate("username", "password");
}

Skin/Cape of a player

Get the skin and/or cape texture URL of a player. This just requires the player's UUID or username.

void main() async {
    // PlayerUUID is a Pair<String, String>
    PlayerUuid player = await getUuid('<your username>');
    Profile profile = await getProfile(player.second);
    String url = profile.textures.getSkinUrl();
}

Reading NBT data

Read NBT data from a local file. This supports the full NBT specification, however support for SNBT is not implemented yet. A full list of tags/types usable in this context can be found here.

void main() async {
    // You can create a NbtFile object from a File object or
    // from a String path.
    final nbtReader = NbtReader.fromFile('yourfile.nbt');
    await nbtReader.read();
    NbtCompound rootNode = nbtReader.root;
    // You can now read information from your [rootNode].
    // for example, rootNode[0] will return the first child,
    // if present.
    print(rootNode.first);
    print(rootNode.getChildrenByTag(NbtTagType.TAG_STRING));
}

Pinging a server

Pings the Minecraft: Java Edition (1.6+) server at 'mc.hypixel.net'. You can use any DNS or IP Address to ping them. The server will provide basic information, as the player count, ping and MOTD.

void main() async {
	/// Pinging a server and getting its basic information is 
	/// as easy as that.
	final server = await ping('mc.hypixel.net');
	if (server == null || server.response == null) return;
	print('Latency: ${server.ping}');
	final players = ping.response!.players;
	print('${players.online} / ${players.max}'); // e.g. 5 / 20
}

License

The MIT License, see LICENSE.

dart_minecraft's People

Contributors

dependabot[bot] avatar spnda avatar thekingdave avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dart_minecraft's Issues

Can't read player.dat file on web

I'm trying to read a Minecraft playerdata file using Flutter Web, and I'm running into a couple issues:

  1. Apparently the file I was trying to read was compressed as gzip. Digging into the code, I saw this comment:
    "On JS platforms, this does nothing, as it requires converters from 'dart:io'."
    Not a huge problem for me though, as I can detect the compression type and just use https://pub.dev/packages/archive which does support web.

  2. After decompressing, I'm getting "Error: Unsupported operation: Int64 accessor not supported by dart2js." which is from this line: https://github.com/spnda/dart_minecraft/blob/main/lib/src/utilities/readers/_byte_reader.dart#L61
    I also found this related issue: dart-lang/sdk#10275
    Is there anything we can do about this?

Can't ping server

void getServer() async {
    final server = await ping('mc.hypixel.net');
    if (server == null || server.response == null) return;
    final players = server.response!.players;
    if (kDebugMode) {
      print('Pinged ...');
    }
    if (kDebugMode) {
      print('${players.online} / ${players.max}. ${server.ping}ms.');
    }
    setState(() {
      playersOnline = players;
    });
  }

Error: Unsupported operation: dart:io is required to ping servers.

Even when i import dart:io, it still doesnt work

Can you support to read NBT as Json map?

I was checking out your repo and thought it would be awesome if you could add support for reading NBT files and converting them to JSON. It would make working with NBT data a lot easier for people like me who need to create a user interface like this.

Screenshot 2024-09-25 at 15 14 25

can't read file

I have the file in my folders but every time I try to read the nbt file I get error (E/flutter ( 5298): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PathNotFoundException: Cannot open file, path = 'assets/gifs/deneme1.nbt' (OS Error: No such file or directory, errno = 2))

void asd() async {
    print("executed main");
    final nbtReader = NbtReader.fromFile('assets/gifs/deneme1.nbt');
    try {
      nbtReader.read();
    } on NbtFileReadException {
      print('Failed to read data.nbt');
    }
  }

NBTReader.getContentEndian() returns Endian.big without condition

In lib/src/nbt/nbt_reader.dart, class NbtReader
Line 70 to 72

Endian getContentEndian() { return Endian.big; }

This code is going to cause that whenever I try to get the endian of a NBT file, I would get Endian.big.
Is this an oversight? Or maybe you just don't intend to implement this function?

Also, can you please add an argument that receives Endian type variable to NbtWriter().writeFile() to enable us to write NBT file in little endian? I read a NBT file in little endian then write it as another file but then the new NBT file seems broken.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.