Code Monkey home page Code Monkey logo

easy-item's Introduction

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");
    }
}

easy-item's People

Contributors

cerus avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

easy-item's Issues

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.