Guest User

Untitled

a guest
Jun 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. package flavor.pie.testplugin;
  2.  
  3. import org.spongepowered.api.Sponge;
  4. import org.spongepowered.api.command.CommandResult;
  5. import org.spongepowered.api.command.CommandSource;
  6. import org.spongepowered.api.command.args.CommandContext;
  7. import org.spongepowered.api.command.spec.CommandSpec;
  8. import org.spongepowered.api.entity.living.player.Player;
  9. import org.spongepowered.api.event.Listener;
  10. import org.spongepowered.api.event.filter.cause.First;
  11. import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
  12. import org.spongepowered.api.event.item.inventory.ClickInventoryEvent;
  13. import org.spongepowered.api.item.inventory.Inventory;
  14. import org.spongepowered.api.item.inventory.InventoryArchetypes;
  15. import org.spongepowered.api.item.inventory.Slot;
  16. import org.spongepowered.api.item.inventory.property.InventoryDimension;
  17. import org.spongepowered.api.item.inventory.property.SlotIndex;
  18. import org.spongepowered.api.item.inventory.transaction.SlotTransaction;
  19. import org.spongepowered.api.plugin.Plugin;
  20. import org.spongepowered.api.text.Text;
  21.  
  22. import java.util.Optional;
  23.  
  24. @Plugin(id = "testplugin")
  25. public class TestPlugin {
  26.  
  27. @Listener
  28. public void preInit(GamePreInitializationEvent e) {
  29. Sponge.getCommandManager().register(this, CommandSpec.builder()
  30. .child(CommandSpec.builder().executor(this::testCustom).build(), "custom")
  31. .child(CommandSpec.builder().executor(this::testHopper).build(), "hopper")
  32. .child(CommandSpec.builder().executor(this::testDispenser).build(), "dispenser")
  33. .build(), "test");
  34. }
  35.  
  36. public CommandResult testCustom(CommandSource src, CommandContext args) {
  37. Player p = (Player) src;
  38. p.openInventory(Inventory.builder().property(InventoryDimension.of(9, 3)).build(this));
  39. return CommandResult.success();
  40. }
  41.  
  42. public CommandResult testHopper(CommandSource src, CommandContext args) {
  43. Player p = (Player) src;
  44. p.openInventory(Inventory.builder().of(InventoryArchetypes.HOPPER).build(this));
  45. return CommandResult.success();
  46. }
  47.  
  48. public CommandResult testDispenser(CommandSource src, CommandContext args) {
  49. Player p = (Player) src;
  50. p.openInventory(Inventory.builder().of(InventoryArchetypes.DISPENSER).build(this));
  51. return CommandResult.success();
  52. }
  53.  
  54. @Listener
  55. public void onClick(ClickInventoryEvent e, @First Player p) {
  56. SlotTransaction transaction = e.getTransactions().get(0);
  57. Slot slot = transaction.getSlot();
  58. Optional<SlotIndex> index = slot.getProperty(SlotIndex.class, SlotIndex.getDefaultKey(SlotIndex.class));
  59. p.sendMessage(Text.of("Clicked slot: " + (index.map(
  60. slotIndex -> (slotIndex.getValue() == null) ? "Null" : slotIndex.getValue()).orElse("Undefined"))));
  61. index = slot.transform().getProperty(SlotIndex.class, SlotIndex.getDefaultKey(SlotIndex.class));
  62. p.sendMessage(Text.of("Clicked slot (transformed): " + (index.map(
  63. slotIndex -> (slotIndex.getValue() == null) ? "Null" : slotIndex.getValue()).orElse("Undefined"))));
  64. }
  65.  
  66. }
Add Comment
Please, Sign In to add comment