Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package flavor.pie.testplugin;
- import org.spongepowered.api.Sponge;
- import org.spongepowered.api.command.CommandResult;
- import org.spongepowered.api.command.CommandSource;
- import org.spongepowered.api.command.args.CommandContext;
- import org.spongepowered.api.command.spec.CommandSpec;
- import org.spongepowered.api.entity.living.player.Player;
- import org.spongepowered.api.event.Listener;
- import org.spongepowered.api.event.filter.cause.First;
- import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
- import org.spongepowered.api.event.item.inventory.ClickInventoryEvent;
- import org.spongepowered.api.item.inventory.Inventory;
- import org.spongepowered.api.item.inventory.InventoryArchetypes;
- import org.spongepowered.api.item.inventory.Slot;
- import org.spongepowered.api.item.inventory.property.InventoryDimension;
- import org.spongepowered.api.item.inventory.property.SlotIndex;
- import org.spongepowered.api.item.inventory.transaction.SlotTransaction;
- import org.spongepowered.api.plugin.Plugin;
- import org.spongepowered.api.text.Text;
- import java.util.Optional;
- @Plugin(id = "testplugin")
- public class TestPlugin {
- @Listener
- public void preInit(GamePreInitializationEvent e) {
- Sponge.getCommandManager().register(this, CommandSpec.builder()
- .child(CommandSpec.builder().executor(this::testCustom).build(), "custom")
- .child(CommandSpec.builder().executor(this::testHopper).build(), "hopper")
- .child(CommandSpec.builder().executor(this::testDispenser).build(), "dispenser")
- .build(), "test");
- }
- public CommandResult testCustom(CommandSource src, CommandContext args) {
- Player p = (Player) src;
- p.openInventory(Inventory.builder().property(InventoryDimension.of(9, 3)).build(this));
- return CommandResult.success();
- }
- public CommandResult testHopper(CommandSource src, CommandContext args) {
- Player p = (Player) src;
- p.openInventory(Inventory.builder().of(InventoryArchetypes.HOPPER).build(this));
- return CommandResult.success();
- }
- public CommandResult testDispenser(CommandSource src, CommandContext args) {
- Player p = (Player) src;
- p.openInventory(Inventory.builder().of(InventoryArchetypes.DISPENSER).build(this));
- return CommandResult.success();
- }
- @Listener
- public void onClick(ClickInventoryEvent e, @First Player p) {
- SlotTransaction transaction = e.getTransactions().get(0);
- Slot slot = transaction.getSlot();
- Optional<SlotIndex> index = slot.getProperty(SlotIndex.class, SlotIndex.getDefaultKey(SlotIndex.class));
- p.sendMessage(Text.of("Clicked slot: " + (index.map(
- slotIndex -> (slotIndex.getValue() == null) ? "Null" : slotIndex.getValue()).orElse("Undefined"))));
- index = slot.transform().getProperty(SlotIndex.class, SlotIndex.getDefaultKey(SlotIndex.class));
- p.sendMessage(Text.of("Clicked slot (transformed): " + (index.map(
- slotIndex -> (slotIndex.getValue() == null) ? "Null" : slotIndex.getValue()).orElse("Undefined"))));
- }
- }
Add Comment
Please, Sign In to add comment