QtCraft API C++ classes QML types Handbook

QcAdventureRules Class

The allow-lists of adventure mode (decision E-735), a setting of the world: for each item kind, the blocks it may break and the blocks it may act on (be placed on, till, light, pour, …), like the reference's can_break and can_place_on, but per item kind instead of per stack. More...

Header: #include <common/gameplay/QcAdventureRules.h>

Public Types

struct Read
enum class Action { Break, PlaceOn }

Public Functions

bool allow(QcAdventureRules::Action action, const QString &item, const QStringList &blocks)
QStringList blocks(QcAdventureRules::Action action, const QString &item) const
void clear()
bool isEmpty() const
QStringList items(QcAdventureRules::Action action) const
void merge(const QcAdventureRules &other)
int nameCount() const
bool permits(QcGameMode mode, QcAdventureRules::Action action, const QcItemDefinition *held, const QcBlockDefinition &block) const
bool remove(QcAdventureRules::Action action, const QString &item, const QStringList &blocks)
QJsonObject toJson() const
void write(QDataStream &stream) const

Static Public Members

const int actionCount
const char *const anyItem
QcAdventureRules::Read fromJson(const QJsonObject &json)
bool isValidName(const QString &name)
const int maxBlocks
const int maxItems
const int maxNameLength
const int maxNames
QLatin1StringView name(QcAdventureRules::Action action)
std::optional<QcAdventureRules::Action> parseAction(QStringView text)
std::optional<QcAdventureRules> read(QDataStream &stream)

Detailed Description

The kind anyItem ("*") stands for every item and the empty hand. Saved in world.json ("adventure"), changed with /adventure and sent to adventure players with their mode (QcMsg::GameMode), so server and client decide with the same lists (permits). Names are kept as written: a block or item of a missing mod stays in the list and matches nothing.

Member Type Documentation

enum class QcAdventureRules::Action

What an allow-list permits.

ConstantValueDescription
QcAdventureRules::Action::Break0Mining a block (also putting out fire, which is mined like a block).
QcAdventureRules::Action::PlaceOn1Using the held item on a block that does not react to the use itself: placing a block, tilling, flattening, carving, fertilizing, lighting fire, pouring or scooping a liquid, placing an armour stand or a minecart.

Member Function Documentation

bool QcAdventureRules::allow(QcAdventureRules::Action action, const QString &item, const QStringList &blocks)

Adds blocks to the list of item for action. Returns false (nothing changed) when a name is not valid or a limit would be passed.

QStringList QcAdventureRules::blocks(QcAdventureRules::Action action, const QString &item) const

Returns the blocks listed for the item kind item (or anyItem) and action, sorted.

void QcAdventureRules::clear()

Empties every list.

[static] QcAdventureRules::Read QcAdventureRules::fromJson(const QJsonObject &json)

Returns the lists of json in the form of toJson(), with what was left out (Read): an action that is no object, and an item whose list is no list of block names or passes a limit.

bool QcAdventureRules::isEmpty() const

Returns whether no action has a list.

[static] bool QcAdventureRules::isValidName(const QString &name)

Returns whether name can stand in a list: not empty and at most maxNameLength characters (any name of content or "*").

QStringList QcAdventureRules::items(QcAdventureRules::Action action) const

Returns the item kinds that have a list for action, sorted.

void QcAdventureRules::merge(const QcAdventureRules &other)

Adds every entry of other; an item's list that would pass a limit is left out.

[static] QLatin1StringView QcAdventureRules::name(QcAdventureRules::Action action)

Returns the name of action: "canBreak" and "canPlaceOn", the keys of world.json and the words of /adventure.

int QcAdventureRules::nameCount() const

Returns the item kinds and blocks of both actions, counted against maxNames.

[static] std::optional<QcAdventureRules::Action> QcAdventureRules::parseAction(QStringView text)

Returns the action named text (case-insensitive); std::nullopt for another word.

bool QcAdventureRules::permits(QcGameMode mode, QcAdventureRules::Action action, const QcItemDefinition *held, const QcBlockDefinition &block) const

Returns whether a player in mode may do action with held (none: empty hand) to block. True in every mode without allow-lists (QcGameModes::Rules::buildByAllowList); in adventure only when the held kind or anyItem lists the block. Filling a bottle changes no block and is always allowed.

[static] std::optional<QcAdventureRules> QcAdventureRules::read(QDataStream &stream)

Returns the lists read from stream in the format of write(); std::nullopt for counts or names beyond the limits.

bool QcAdventureRules::remove(QcAdventureRules::Action action, const QString &item, const QStringList &blocks)

Takes blocks off the list of item for action, all of them when blocks is empty. Returns false when the item had none of them.

QJsonObject QcAdventureRules::toJson() const

Returns the lists as world.json "adventure": {"canBreak": {item: [blocks]}, "canPlaceOn": {…}} (also in structure files).

void QcAdventureRules::write(QDataStream &stream) const

Writes the lists to stream in the wire format of QcMsg::GameMode.

Member Variable Documentation

const int QcAdventureRules::actionCount

The number of actions.

const char *const QcAdventureRules::anyItem

The item kind that stands for every item and the empty hand.

const int QcAdventureRules::maxNames

All names together (item kinds and blocks of both actions): keeps the encoded lists at about 1 MiB (4 + 2 x 128 bytes a name), far below a protocol frame (QcProtocol::maxFrameBytes).