Giter Site home page Giter Site logo

simpleinvapi's Introduction

SimpleInvApi

A very simple inventory api that is based on nms

I just got really tired of the nonsense of bukkit inventory events and no library offered proper good user experience and flexibility

Note

This was made mostly for internal use, but you can use if you also like this

build.gradle.kts

repositories {
    maven("https://repo.masmc05.dev/repository/maven-snapshots/")
}

dependencies {
    compileOnly("dev.masmc05:SimpleInvApi:0.1-SNAPSHOT")
}

Here's an example usage of the API

public class ExampleCreator implements InventoryViewConstructor {
    private final AtomicInteger offset = new AtomicInteger(0);
    //Yes, bukkit inventory (craft inventory class), fully working, except for player.openInventory on non standard sizes
    private final Inventory topInventory = InventoryApi.createFlexibleInventory(120);
    private final Player player;

    public ExampleCreator(Player player) {
        this.player = player;
        for (int i = 1; i <= 64; i++) {
            topInventory.setItem(i - 1, new ItemStack(Material.DIAMOND, i));
        }
        for (int i = 64; i < 120; i++) {
            topInventory.setItem(i, new ItemStack(Material.EMERALD, i - 63));
        }
    }
    @Override
    public @NotNull Inventory getTopInventory() {
        return this.topInventory;
    }

    @Override
    public @NotNull PlayerInventory getPlayerInventory() {
        return this.player.getInventory();
    }

    @Override
    public int getTopSize() {
        return 18; //Can be any standart chest sizes
    }

    @Override
    public @NotNull Slot createTopSlot(int index) {
        return new Slot() {
            @Override
            public @NotNull Inventory inventory() {
                return ExampleCreator.this.topInventory;
            }

            @Override
            public int index() {
                return ExampleCreator.this.offset.get() + index;
            }

            @Override
            public boolean canPlace(@NotNull ItemStack itemStack) {
                return false;
            }

            @Override
            public boolean canTake() {
                return false;
            }

            @Override
            public void changed() {
                offset.incrementAndGet();
                player.updateInventory();
            }
        };
    }

    @Override
    public @NotNull Slot createPlayerInventorySlot(int index) {
        //This allows visually changing the slots in player's inventory without actually affecting them
        //Also this needs to be used to listen to clicks
        return new DummySlot(new ItemStack(Material.YELLOW_STAINED_GLASS, index));
    }
    /*
    Use this if you only want to listen to the click
    @Override
    public @NotNull Slot createPlayerInventorySlot(int index) {
        return new SlotAccess(this.player.getInventory(), index, () -> {Action});
    }
    */

    @Override
    public @NotNull Slot createHotbarSlot(int index) {
        //Same as with createPlayerInventorySlot but with hotbar
        return new DummySlot(new ItemStack(Material.BLACK_STAINED_GLASS, index));
    }
}

And then open it using

ExampleCreator creator = new ExampleCreator(player);
InventoryApi.createAndOpen(player, creator);

simpleinvapi's People

Contributors

masmc05 avatar

Watchers

 avatar

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.