A small library for parsing ItemStacks from a human-readable format

Overview

easy-item

A small library for parsing ItemStacks from a human-readable format (1.16.5+, Java 11)

TODO: Maybe add serialization (item to human-readable string)?

Examples

my-cool-item: |-
  diamond_sword with name "Sword of doom"
  with enchantment "sharpness" 16
  with lore "&c&lIncredible sword"
  with lore "&7Made by easy-item"
my-other-cool-item: "minecraft:player_head with owner \"Cerus_\""
pants: |-
  leather_leggings with color "#00AFFE"

Installation

https://jitpack.io/#cerus/easy-item

<project>
    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.github.cerus</groupId>
            <artifactId>easy-item</artifactId>
            <version>Tag</version>
        </dependency>
    </dependencies>
</project>

Available transformers

Amount
with amount INTEGER
with amount 32

Enchantment
with enchantment STRING INTEGER
with enchantment "minecraft:sharpness" 2

Flag
with flag STRING
with flag "HIDE_ENCHANTS"

Leather-color
with color/leathercolor STRING
with color/leathercolor "#00AFFE"
with color/leathercolor "255,255,255"

Skullowner
with skullowner/owner STRING
with skullowner/owner "Cerus_"
with skullowner/owner "06f8c3cc-a3c5-4b48-bc6d-d3ee8963f2af"

Usage

Parsing an item:

import dev.cerus.easyitem.EasyItem;
import dev.cerus.easyitem.tokenizer.Token;
import dev.cerus.easyitem.tokenizer.Tokenizer;
import dev.cerus.easyitem.exception.ParserException;
import dev.cerus.easyitem.parser.Parser;
import dev.cerus.easyitem.parser.ParserBuilder;
import java.util.List;

class MyClass {

    public ItemStack makeStackQuick(String str) {
        // You could also use EasyItem#parse(String) but that 
        // would require you to catch a ParserException
        return EasyItem.unsafeParse(str);
    }

    public ItemStack makeStack(String str) {
        try {
            List<Token<?>> tokens = new Tokenizer(str)
                    .tokenize();
            Parser parser = new ParserBuilder(tokens)
                    .defaultTransformers()
                    .transformers(/*Add additional transformers here*/)
                    .build();
            // EasyItem#newDefaultParser(List<Token<?>>) also 
            // returns this exact parser
            return parser.parse();
        } catch (ParserException ex) {
            ex.printStackTrace();
            System.err.println("Invalid item string");
            return null;
        }
    }

}

Creating your own transformer:

import dev.cerus.easyitem.tokenizer.Token;
import dev.cerus.easyitem.exception.ParserException;
import dev.cerus.easyitem.parser.Parser;
import dev.cerus.easyitem.parser.Transformer;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

class MyTransformer implements Transformer {

    @Override
    public void transform(ItemStack itemStack, Parser parser) throws ParserException {
        // Consume the two words "with" and "isodd"
        parser.consume();
        parser.consume();

        // Consume next token which should be our number
        Token<?> numToken = parser.consume();
        if (numToken.getType() != Token.Type.INTEGER
                && numToken.getType() != Token.Type.LONG) {
            throw new ParserException("Not a (valid) number");
        }

        // Get number and make odd/even bool
        long num = numToken.getValueUnsafe();
        boolean odd = (num & 1) == 1;

        ItemMeta meta = itemStack.getItemMeta();
        meta.setDisplayName(odd ? "Is odd" : "Is even");
        itemStack.setItemMeta(meta);
    }

    @Override
    public boolean matches(Parser parser) {
        // It's important to check if we do have tokens available before we peek
        return parser.has() // Is parser index < tokens?
                && parser.peek().equals(Token.Type.WORD, "with")
                && parser.has(1) // Is parser index + 1 < tokens?
                && parser.peekNext().equals(Token.Type.WORD, "isodd");
    }
}
You might also like...

Human Resources Management System Using React with Java

Human Resources Management System Using React with Java

File Directory Main Adapters Abstract Concretes Business Abstract Concretes Core / Utilities Regex Results Upload Data Access Abstract Entities Concre

Jul 28, 2021

Java - Backend for Human Resource Management System

HRMS (Human Resource Management System) Main Directory PostgreSQL (Heroku)Live API Java(1.8) - Spring Boot based. Dependencies: Spring Boot DevTools S

Dec 26, 2022

Human Resource Management Application on JavaFX using MVC Design Pattern

Human Resource Management Application on JavaFX using MVC Design Pattern

Human-Resource-Management-System-with-JFoenix Human Resource Management Application on JavaFX using MVC Design Pattern Built With Java JavaFX - FXML C

Mar 18, 2022

This is a small library written in Java for minecraft login.

minecraft-auth-library This is a small library written in Java for minecraft login. Usage: Login with microsoft MinecraftAuthenticator minecraftAuthen

Feb 5, 2022

A small companion library to Mixin, designed to help you write your Mixins in a more expressive and compatible way.

MixinExtras A small companion library to Mixin, designed to help you write your Mixins in a more expressive and compatible way. More information about

Jan 7, 2023

A small library for importing/exporting BigTable instance schemas and row data.

A small library for importing/exporting BigTable instance schemas and row data.

Vandelay An Importer/Exporter for BigTable A small library for BigTable instance and data management. Vandelay allows you to export the full configura

Nov 17, 2022

Convert Journeymap image data into Xaero format for Xaero's minecraft map mod

Convert Journeymap image data into Xaero format for Xaero's minecraft map mod

JMtoXaero JMtoXaero is a tool to convert JourneyMap tiles to regions used by Xaero's World Map Description Reads images from the Journeymap folder Wri

Dec 21, 2022

Split into data blocks,In this format, efficient reading can be realized,Avoid unnecessary data reading operations.

Split into data blocks,In this format, efficient reading can be realized,Avoid unnecessary data reading operations.

dataTear 切换至:中文文档 knowledge base dataTear Split into data fragments for data management. In this format, efficient reading can be achieved to avoid un

Dec 15, 2022

A small mod to improve support for architectures and libraries officially unsupported by Minecraft. Mainly targeting Apple Macs using arm processors.

fabric-loom-native-support A small mod to improve support for architectures and libraries officially unsupported by Minecraft. Mainly targeting Apple

Oct 17, 2022
Owner
Maximilian Dorn
19 y/o professional bug creator from Germany / I like cats, computers, space and open source / Java is my swiss army knife
Maximilian Dorn
Terse. Elegant. Readable. Even faster.

Myxal Terse. Elegant. Readable. Even faster. A top secret experiment to make Jyxal even faster and multitarget compiled. How to run Download the lates

null 6 May 30, 2022
Human Resource Management System (Java & React)

?? HumanResourceManagementSystem ?? Steps İş Arayanlar sisteme kayıt olabilmelidir. ✔️ İş verenler sisteme kayıt olabilmelidir. ✔️ ️ Sisteme genel iş

Furkan Paşaoğlu 5 Sep 12, 2022
Human Resources Management System

Human Resource Management System ( HRMS ) The following tools / languages will be used in this project; Java (Spring Boot based) - on the Backend side

Fatih Deniz 17 Dec 1, 2022
Human Resource Management System - Backend

Human Resource Management System - Backend File Structure ??️ Layered Architecture Entities - The package in which the assets are kept DataAcces - Pac

Tarık Kaan Koç 30 Jan 1, 2023
Human resource management system with java spring

Pair Programming This project developed with Emin Ümüt Erarslan a.k.a 8CA5F İnsan Kaynakları Yönetim Sistemi Bu proje Engin Demiroğ' un Java-React kam

Burak KALAYCI 45 Dec 26, 2022
Backend For Human Resource Management System

?? Presentation This is a hrms project. At the backend of this project I used Java(Spring Boot) and I used PostgreSQL as database management. At the f

Salih Bora Öztürk 13 Aug 5, 2022
A repository that contains the backend part of the Human Resources Management System.

Human Resources Management System Backend A human resources management system is a form of human resources (HR) software that combines several systems

Bulent Baris Kilic 24 Dec 26, 2022
human resourse management software

HRMS İstihdam ve Kariyer Platformu Proje İsterleri Req 1 : İş Arayanlar sisteme kayıt olabilmelidir. Kabul Kriterleri: Kayıt sırasında kullanıcıdan ad

Ceren Susuz 21 Dec 20, 2022
Human Resource Management System

hrms Human Resource Management System ?? Proje Hakkında N-Katmanlı Solid mimari yapısı ile hazırlanan, SpringBoot kullanılarak CRUD işlemlerinin yapıl

Atakan Reyhanioglu 16 Jun 6, 2022
N-Layer Architecture human resource management system project with Java.

HRMS Project Backend N-Layer Architecture human resource management system project with Java. Report Bug · Request Feature About The Project Built Wit

Ahmet Çetinkaya 78 Dec 26, 2022