Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.esinia.server.invintories.craftingtables;
- import org.spongepowered.api.Sponge;
- import org.spongepowered.api.data.key.Keys;
- import org.spongepowered.api.entity.living.player.Player;
- import org.spongepowered.api.event.Listener;
- import org.spongepowered.api.event.filter.Getter;
- import org.spongepowered.api.event.filter.cause.Root;
- import org.spongepowered.api.event.item.inventory.ClickInventoryEvent;
- import org.spongepowered.api.item.ItemTypes;
- import org.spongepowered.api.item.inventory.Inventory;
- import org.spongepowered.api.item.inventory.ItemStack;
- import org.spongepowered.api.text.Text;
- //name of the class file and starts the main method
- public class CraftingTablesRudimentaryInventory {
- //listens for the following event to occur
- @Listener
- //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
- public void onItemCraft(ClickInventoryEvent event, @Root Player player, @Getter("getTargetInventory") Inventory inventory) {
- //Checks the event inventory to see if it contains xyz and if it does it proceeds
- if (inventory.getName().get().contains("Rudimentary Crafting Table")){
- //Sets whether the event is cancelled or not to true
- event.setCancelled(true);
- //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
- if (event.getCursorTransaction().getFinal().get(Keys.DISPLAY_NAME).get().equals(Text.of("Emerald"))){
- //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
- player.getInventory().slots().forEach(slot -> slot.peek().ifPresent(emer -> {
- //if the item type of emer is item type Emerald and...
- if (emer.getItem() == ItemTypes.EMERALD &&
- //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
- emer.get(Keys.DISPLAY_NAME).orElse(Text.EMPTY).toPlain().equals("test")) {
- //Acquires the server broadcast chat and sends the text "Someone Has an Emerald!"
- Sponge.getServer().getBroadcastChannel().send(Text.of("Someone Has An Emerald!"));
- //Creates an Itemstack variable called "stack" and sets it to equal an Itemstack with the itemtype emerald with the quantity of 1
- ItemStack stack = ItemStack.of(ItemTypes.EMERALD, 1);
- //stack.offer(Keys.DISPLAY_NAME, Text.of(TextColors.AQUA, "test"));
- //offers stack the displayname of text "test"
- stack.offer(Keys.DISPLAY_NAME, Text.of("test"));
- //Creates an Inventory variable called "emeralds" and sets it to a replica of the players inventory only including itemstacks that match stack
- Inventory emeralds = player.getInventory().queryAny(stack);
- // creates an integer variable called "total" and sets it to the total amount of items in emeralds
- int total = emeralds.totalItems();
- //if total is great than or less than 5 it proceeds
- if (total >= 5){
- // sends the player an message of text "Removing Items"
- player.sendMessage(Text.of("Removing Items"));
- //pulls 5 items out of emeralds
- emeralds.poll(5);
- //gives the players inventory an itemstack with the type diamond with the quantity of 1
- player.getInventory().offer(ItemStack.of(ItemTypes.DIAMOND, 1));
- //if the previous method is false it proceeds
- } else{
- // sends the player a message with the text of....
- player.sendMessage(Text.of("You don't have enough items. You only have " + total + "."));
- }
- }
- }));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment