Ghaz-ranka

Untitled

May 19th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. package com.esinia.server.invintories.craftingtables;
  2.  
  3. import org.spongepowered.api.Sponge;
  4. import org.spongepowered.api.data.key.Keys;
  5. import org.spongepowered.api.entity.living.player.Player;
  6. import org.spongepowered.api.event.Listener;
  7. import org.spongepowered.api.event.filter.Getter;
  8. import org.spongepowered.api.event.filter.cause.Root;
  9. import org.spongepowered.api.event.item.inventory.ClickInventoryEvent;
  10. import org.spongepowered.api.item.ItemTypes;
  11. import org.spongepowered.api.item.inventory.Inventory;
  12. import org.spongepowered.api.item.inventory.ItemStack;
  13. import org.spongepowered.api.text.Text;
  14.  
  15. //name of the class file and starts the main method
  16. public class CraftingTablesRudimentaryInventory {
  17. //listens for the following event to occur
  18. @Listener
  19. //event that fires when a slot in an inventory is clicked, it filters the root of the event to only players, and sets the inventory variable inventory to the target inventory of the event
  20. public void onItemCraft(ClickInventoryEvent event, @Root Player player, @Getter("getTargetInventory") Inventory inventory) {
  21. //Checks the event inventory to see if it contains xyz and if it does it proceeds
  22. if (inventory.getName().get().contains("Rudimentary Crafting Table")){
  23. //Sets whether the event is cancelled or not to true
  24. event.setCancelled(true);
  25. //Gets the result of the cursor clicking on an item. Then gets the display name key and checks if it is equal to the text "Emerald" and if that is true it proceeds
  26. if (event.getCursorTransaction().getFinal().get(Keys.DISPLAY_NAME).get().equals(Text.of("Emerald"))){
  27. //Gets all the inventory slots of the player and for each slot it looks for an itemstack and if it is present it saves it as "emer" and proceeds
  28. player.getInventory().slots().forEach(slot -> slot.peek().ifPresent(emer -> {
  29. //if the item type of emer is item type Emerald and...
  30. if (emer.getItem() == ItemTypes.EMERALD &&
  31. //the display name of emer doesnt exist it is read at empty and if it does exist it is converted to plain text and checked if it equals "test" and if that is true is proceeds
  32. emer.get(Keys.DISPLAY_NAME).orElse(Text.EMPTY).toPlain().equals("test")) {
  33. //Acquires the server broadcast chat and sends the text "Someone Has an Emerald!"
  34. Sponge.getServer().getBroadcastChannel().send(Text.of("Someone Has An Emerald!"));
  35. //Creates an Itemstack variable called "stack" and sets it to equal an Itemstack with the itemtype emerald with the quantity of 1
  36. ItemStack stack = ItemStack.of(ItemTypes.EMERALD, 1);
  37. //stack.offer(Keys.DISPLAY_NAME, Text.of(TextColors.AQUA, "test"));
  38. //offers stack the displayname of text "test"
  39. stack.offer(Keys.DISPLAY_NAME, Text.of("test"));
  40. //Creates an Inventory variable called "emeralds" and sets it to a replica of the players inventory only including itemstacks that match stack
  41. Inventory emeralds = player.getInventory().queryAny(stack);
  42. // creates an integer variable called "total" and sets it to the total amount of items in emeralds
  43. int total = emeralds.totalItems();
  44. //if total is great than or less than 5 it proceeds
  45. if (total >= 5){
  46. // sends the player an message of text "Removing Items"
  47. player.sendMessage(Text.of("Removing Items"));
  48. //pulls 5 items out of emeralds
  49. emeralds.poll(5);
  50. //gives the players inventory an itemstack with the type diamond with the quantity of 1
  51. player.getInventory().offer(ItemStack.of(ItemTypes.DIAMOND, 1));
  52. //if the previous method is false it proceeds
  53. } else{
  54. // sends the player a message with the text of....
  55. player.sendMessage(Text.of("You don't have enough items. You only have " + total + "."));
  56. }
  57. }
  58. }));
  59. }
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment